Skip to content

Commit

Permalink
Update midi command
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Sep 10, 2013
1 parent c38b279 commit b401a18
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 84 deletions.
Expand Up @@ -1144,22 +1144,30 @@ public void registerCoreMembers() {

// <--[command]
// @Name Midi
// @Usage midi [file:<name>] [<location>/listeners:<player>|...] (tempo:<#.#>)
// @Usage midi [file:<name>] (<location>/<player>|...) (tempo:<#.#>)
// @Required 1
// @Stable Todo
// @Short Todo
// @Author Todo
// @Stable Stable
// @Short Plays a midi file at a given location or to a list of players using note block sounds.
// @Author authorblues
// @Description
// Todo
// This will fully load a midi song file stored in plugins/Denizen/midi/
// The file must be a valid midi file with the extension .mid
// It will continuously play the song as noteblock songs at the given location or group of players until the song ends.
// By default, this will play for the connected player only.
// @Tags
// Todo
// None.
// @Usage
// Todo
// Use to play a midi song file at a given location
// - midi file:Mysong <player.location>
//
// @Usage
// Use to play a midi song file at a given location to the specified player
// - midi file:Mysong <server.list_online_players>
// @Example
// Todo
// -->
registerCoreMember(MidiCommand.class,
"MIDI", "midi [file:<name>] [<location>/listeners:<player>|...] (tempo:<#.#>)", 1);
"MIDI", "midi [file:<name>] (<location>/<player>|...) (tempo:<#.#>)", 1);

// <--[command]
// @Name Mount
Expand Down
@@ -1,6 +1,8 @@
package net.aufdemrand.denizen.scripts.commands.world;

import java.io.File;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

Expand All @@ -10,16 +12,14 @@

import net.aufdemrand.denizen.exceptions.CommandExecutionException;
import net.aufdemrand.denizen.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizen.objects.aH;
import net.aufdemrand.denizen.objects.aH.ArgumentType;
import net.aufdemrand.denizen.objects.*;
import net.aufdemrand.denizen.objects.dEntity;
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.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
Expand All @@ -44,99 +44,76 @@ 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>();
for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) {
if (!scriptEntry.hasObject("location") &&
arg.matchesArgumentType(dLocation.class))
scriptEntry.addObject("location", arg.asType(dLocation.class));

// Iterate through arguments
for (String arg : scriptEntry.getArguments()){
if (aH.matchesLocation(arg))
location = aH.getLocationFrom(arg);
else if (!scriptEntry.hasObject("listeners") &&
arg.matchesArgumentList(dPlayer.class))
scriptEntry.addObject("listeners", arg.asType(dList.class));

else if (aH.matchesValueArg("file, f", arg, ArgumentType.Custom)) {
try {
String path = denizen.getDataFolder() +
File.separator + "midi" +
File.separator + aH.getStringFrom(arg);
else if (!scriptEntry.hasObject("tempo") &&
arg.matchesPrimitive(aH.PrimitiveType.Double))
scriptEntry.addObject("tempo", arg.asElement());

if (!path.endsWith(".mid")) {
else if (!scriptEntry.hasObject("file")) {
String path = denizen.getDataFolder() +
File.separator + "midi" +
File.separator + arg.getValue();
if (!path.endsWith(".mid"))
path = path + ".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);
}
}
scriptEntry.addObject("file", new Element(path));
}

else if (aH.matchesValueArg("tempo, t", arg, ArgumentType.Float))
tempo = aH.getFloatFrom(arg);

else throw new InvalidArgumentsException(Messages.ERROR_UNKNOWN_ARGUMENT, arg);
else throw new InvalidArgumentsException(Messages.ERROR_UNKNOWN_ARGUMENT, arg.raw_value);
}

// Check required args
if (file == null)
if (!scriptEntry.hasObject("file"))
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().getPlayerEntity());
}
if (!scriptEntry.hasObject("location") &&
!scriptEntry.hasObject("listeners"))
scriptEntry.addObject("listeners", new dList(scriptEntry.getPlayer().identify()));

// Stash args in ScriptEntry for use in execute()
scriptEntry.addObject("file", file);
scriptEntry.addObject("listeners", listeners);
scriptEntry.addObject("location", location);
scriptEntry.addObject("tempo", tempo);
if (!scriptEntry.hasObject("tempo"))
scriptEntry.addObject("tempo", new Element(1));
}

@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");
File file;
try {
file = new File(scriptEntry.getElement("file").asString());
}
catch (Exception ex) {
dB.echoError("Invalid file " + scriptEntry.getElement("file").asString());
return;
}
dList listeners = (dList) scriptEntry.getObject("listeners");
dLocation location = (dLocation) scriptEntry.getObject("location");
float tempo = (float) scriptEntry.getElement("tempo").asDouble();

// 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));
aH.debugObj("file", file.getPath()) +
(listeners != null ? listeners.debug() : "") +
(location != null ? location.debug() : "") +
aH.debugObj("Tempo", tempo));

// Play the sound
if (location != null) {
MidiUtil.playMidiQuietly(file, tempo, location);
}
else {
MidiUtil.playMidiQuietly(file, tempo, listeners);
HashSet<Player> listenerSet = new HashSet<Player>();
for (String player: listeners.toArray()) {
listenerSet.add(dPlayer.valueOf(player).getPlayerEntity());
}
MidiUtil.playMidiQuietly(file, tempo, listenerSet);
}
}
}
Expand Up @@ -352,7 +352,7 @@ public void blockDamage(BlockDamageEvent event) {
if (determination.toUpperCase().startsWith("INSTABREAK"))
event.setInstaBreak(true);
}

// <--[event]
// @Events
// block fades
Expand All @@ -369,21 +369,21 @@ public void blockDamage(BlockDamageEvent event) {
// -->
@EventHandler
public void blockFade(BlockFadeEvent event) {

Map<String, dObject> context = new HashMap<String, dObject>();
dMaterial material = new dMaterial(event.getBlock().getType());

context.put("location", new dLocation(event.getBlock().getLocation()));
context.put("material", material);

String determination = doEvents(Arrays.asList
("block fades",
material.name() + " fades"),
null, null, context);

if (determination.toUpperCase().startsWith("CANCELLED"))
event.setCancelled(true);

}

// <--[event]
Expand Down

0 comments on commit b401a18

Please sign in to comment.