Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Midi command to let you play midi files. Big thanks to authorblue…
…s for letting us use his great midi code. Usage: - midi file:pacman
- Loading branch information
1 parent
e11de6d
commit c1b3c34
Showing
6 changed files
with
408 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
140 changes: 140 additions & 0 deletions
140
src/main/java/net/aufdemrand/denizen/scripts/commands/core/MidiCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
package net.aufdemrand.denizen.scripts.commands.core; | ||
|
||
import java.io.File; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
import org.bukkit.Location; | ||
import org.bukkit.entity.Entity; | ||
import org.bukkit.entity.Player; | ||
|
||
import net.aufdemrand.denizen.exceptions.CommandExecutionException; | ||
import net.aufdemrand.denizen.exceptions.InvalidArgumentsException; | ||
import net.aufdemrand.denizen.scripts.ScriptEntry; | ||
import net.aufdemrand.denizen.scripts.commands.AbstractCommand; | ||
import net.aufdemrand.denizen.utilities.midi.MidiUtil; | ||
import net.aufdemrand.denizen.utilities.arguments.aH; | ||
import net.aufdemrand.denizen.utilities.arguments.dEntity; | ||
import net.aufdemrand.denizen.utilities.arguments.aH.ArgumentType; | ||
import net.aufdemrand.denizen.utilities.debugging.dB; | ||
import net.aufdemrand.denizen.utilities.debugging.dB.Messages; | ||
|
||
/* midi [file:<name>] (listener(s):[p@<name>|...])|(location:<x,y,z,world>) (tempo:<#.#>) */ | ||
|
||
/** | ||
* Arguments: [] - Required, () - Optional | ||
* [file:<name>] specifies the name of the file under plugins/Denizen/midi/ | ||
* (listener(s):[p@<name>|...]) specifies the players who will listen to the midi | ||
* (location:<x,y,z,world>) specifies the location where the midi will be played | ||
* [tempo:<#.#>] sets the tempo of the midi | ||
* | ||
* The listeners and location arguments cannot be used at the same time, but | ||
* the location has a higher priority if both are included. | ||
* | ||
* Example Usage: | ||
* midi "file:stillalive" "tempo:1.0" | ||
* midi "file:mariotheme" listeners:p@aufdemrand|p@Jeebiss | ||
* midi "file:clairdelune" location:200,63,200,world | ||
* | ||
* @author authorblues, David Cernat | ||
*/ | ||
|
||
public class MidiCommand extends AbstractCommand { | ||
|
||
@Override | ||
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException { | ||
|
||
// Initialize fields | ||
File file = null; | ||
float tempo = 1.0F; | ||
Location location = null; | ||
Set<Player> listeners = new HashSet<Player>(); | ||
|
||
// Iterate through arguments | ||
for (String arg : scriptEntry.getArguments()){ | ||
if (aH.matchesLocation(arg)) | ||
location = aH.getLocationFrom(arg); | ||
|
||
else if (aH.matchesValueArg("file, f", arg, ArgumentType.Custom)) { | ||
try { | ||
String path = "plugins/Denizen/midi/" + aH.getStringFrom(arg); | ||
|
||
if (!path.endsWith(".mid")) { | ||
|
||
path = path + ".mid"; | ||
} | ||
|
||
file = new File(path); | ||
} catch (Exception e) { | ||
dB.echoError("Invalid file!"); | ||
} | ||
} | ||
|
||
else if (aH.matchesValueArg("listeners, l", arg, ArgumentType.Custom)) { | ||
|
||
Entity entity = null; | ||
|
||
for (String listener : aH.getListFrom(arg)) { | ||
|
||
entity = dEntity.valueOf(listener).getBukkitEntity(); | ||
|
||
if (entity != null && entity instanceof Player) { | ||
|
||
listeners.add((Player) entity); | ||
} | ||
else { | ||
dB.echoError("Invalid listener '%s'!", listener); | ||
} | ||
} | ||
} | ||
|
||
else if (aH.matchesValueArg("tempo, t", arg, ArgumentType.Float)) | ||
tempo = aH.getFloatFrom(arg); | ||
|
||
else throw new InvalidArgumentsException(Messages.ERROR_UNKNOWN_ARGUMENT, arg); | ||
} | ||
|
||
// Check required args | ||
if (file == null) | ||
throw new InvalidArgumentsException(Messages.ERROR_MISSING_OTHER, "FILE"); | ||
|
||
// If there are no listeners, and the location is null, | ||
// add this player to the listeners | ||
if (location == null && listeners.size() == 0) { | ||
|
||
listeners.add(scriptEntry.getPlayer()); | ||
} | ||
|
||
// Stash args in ScriptEntry for use in execute() | ||
scriptEntry.addObject("file", file); | ||
scriptEntry.addObject("listeners", listeners); | ||
scriptEntry.addObject("location", location); | ||
scriptEntry.addObject("tempo", tempo); | ||
} | ||
|
||
@Override | ||
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException { | ||
|
||
// Extract objects from ScriptEntry | ||
File file = (File) scriptEntry.getObject("file"); | ||
@SuppressWarnings("unchecked") | ||
Set<Player> listeners = (Set<Player>) scriptEntry.getObject("listeners"); | ||
Location location = (Location) scriptEntry.getObject("location"); | ||
Float tempo = (Float) scriptEntry.getObject("tempo"); | ||
|
||
// Report to dB | ||
dB.report(getName(), | ||
aH.debugObj("Playing midi file", file.getPath() | ||
+ (listeners != null ? aH.debugObj("Listeners", listeners) : "") | ||
+ (location != null ? aH.debugObj("Location", location) : "")) | ||
+ aH.debugObj("Tempo", tempo)); | ||
|
||
// Play the sound | ||
if (location != null) { | ||
MidiUtil.playMidiQuietly(file, tempo, location); | ||
} | ||
else { | ||
MidiUtil.playMidiQuietly(file, tempo, listeners); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
src/main/java/net/aufdemrand/denizen/utilities/midi/MidiUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package net.aufdemrand.denizen.utilities.midi; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.Set; | ||
|
||
import javax.sound.midi.InvalidMidiDataException; | ||
import javax.sound.midi.MidiSystem; | ||
import javax.sound.midi.MidiUnavailableException; | ||
import javax.sound.midi.Receiver; | ||
import javax.sound.midi.Sequencer; | ||
|
||
import org.bukkit.Location; | ||
import org.bukkit.Sound; | ||
import org.bukkit.entity.Player; | ||
|
||
/** | ||
* Utility for playing midi files for players to hear. | ||
* | ||
* @author authorblues | ||
*/ | ||
public class MidiUtil | ||
{ | ||
public static void startSequencer(File file, float tempo, Receiver receiver) | ||
throws InvalidMidiDataException, IOException, MidiUnavailableException | ||
{ | ||
|
||
Sequencer sequencer = MidiSystem.getSequencer(false); | ||
sequencer.setSequence(MidiSystem.getSequence(file)); | ||
sequencer.open(); | ||
|
||
// Set desired tempo | ||
sequencer.setTempoFactor(tempo); | ||
|
||
sequencer.getTransmitter().setReceiver(receiver); | ||
sequencer.start(); | ||
} | ||
|
||
public static void playMidi(File file, float tempo, Set<Player> listeners) | ||
throws InvalidMidiDataException, IOException, MidiUnavailableException | ||
{ | ||
NoteBlockReceiver noteblockRecv = new NoteBlockReceiver(listeners); | ||
startSequencer(file, tempo, noteblockRecv); | ||
} | ||
|
||
public static void playMidi(File file, float tempo, Location location) | ||
throws InvalidMidiDataException, IOException, MidiUnavailableException | ||
{ | ||
NoteBlockReceiver noteblockRecv = new NoteBlockReceiver(location); | ||
startSequencer(file, tempo, noteblockRecv); | ||
} | ||
|
||
public static boolean playMidiQuietly(File file, float tempo, Set<Player> listeners) | ||
{ | ||
try { MidiUtil.playMidi(file, tempo, listeners); } | ||
catch (MidiUnavailableException e) { e.printStackTrace(); return false; } | ||
catch (InvalidMidiDataException e) { e.printStackTrace(); return false; } | ||
catch (IOException e) { e.printStackTrace(); return false; } | ||
|
||
return true; | ||
} | ||
|
||
public static boolean playMidiQuietly(File file, float tempo, Location location) | ||
{ | ||
try { MidiUtil.playMidi(file, tempo, location); } | ||
catch (MidiUnavailableException e) { e.printStackTrace(); return false; } | ||
catch (InvalidMidiDataException e) { e.printStackTrace(); return false; } | ||
catch (IOException e) { e.printStackTrace(); return false; } | ||
|
||
return true; | ||
} | ||
|
||
public static boolean playMidiQuietly(File file, Set<Player> listeners) | ||
{ | ||
return playMidiQuietly(file, 1.0f, listeners); | ||
} | ||
|
||
public static boolean playMidiQuietly(File file, Location location) | ||
{ | ||
return playMidiQuietly(file, 1.0f, location); | ||
} | ||
|
||
// provided by github.com/sk89q/craftbook | ||
private static final int[] instruments = { | ||
0, 0, 0, 0, 0, 0, 0, 5, // 8 | ||
6, 0, 0, 0, 0, 0, 0, 0, // 16 | ||
0, 0, 0, 0, 0, 0, 0, 5, // 24 | ||
5, 5, 5, 5, 5, 5, 5, 5, // 32 | ||
6, 6, 6, 6, 6, 6, 6, 6, // 40 | ||
5, 5, 5, 5, 5, 5, 5, 2, // 48 | ||
5, 5, 5, 5, 0, 0, 0, 0, // 56 | ||
0, 0, 0, 0, 0, 0, 0, 0, // 64 | ||
0, 0, 0, 0, 0, 0, 0, 0, // 72 | ||
0, 0, 0, 0, 0, 0, 0, 0, // 80 | ||
0, 0, 0, 0, 0, 0, 0, 0, // 88 | ||
0, 0, 0, 0, 0, 0, 0, 0, // 96 | ||
0, 0, 0, 0, 0, 0, 0, 0, // 104 | ||
0, 0, 0, 0, 0, 0, 0, 0, // 112 | ||
1, 1, 1, 3, 1, 1, 1, 5, // 120 | ||
1, 1, 1, 1, 1, 2, 4, 3, // 128 | ||
}; | ||
|
||
public static Sound patchToInstrument(int patch) | ||
{ | ||
// look up the instrument matching the patch | ||
switch (instruments[patch]) | ||
{ | ||
case 1: return Sound.NOTE_BASS_GUITAR; | ||
case 2: return Sound.NOTE_SNARE_DRUM; | ||
case 3: return Sound.NOTE_STICKS; | ||
case 4: return Sound.NOTE_BASS_DRUM; | ||
case 5: return Sound.NOTE_PLING; | ||
case 6: return Sound.NOTE_BASS; | ||
} | ||
|
||
// if no instrument match is found, use piano | ||
return Sound.NOTE_PIANO; | ||
} | ||
} | ||
|
Oops, something went wrong.