Skip to content

Commit

Permalink
Add Cancel argument to Midi command.
Browse files Browse the repository at this point in the history
  • Loading branch information
davidcernat committed Sep 25, 2013
1 parent 47d227e commit 9ccbcfe
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 25 deletions.
Expand Up @@ -59,7 +59,7 @@ public void registerCoreMembers() {
// @Name Age
// @Usage age [<entity>|...] (adult/baby/<age>) (lock)
// @Required 1
// @Stable Todo
// @Stable stable
// @Short Sets the ages of a list of entities, optionally locking them in those ages.
// @Author David Cernat
// @Description
Expand All @@ -77,7 +77,7 @@ public void registerCoreMembers() {

// @Usage anchor [id:<name>] [assume/remove/add <location>/walkto/walknear (r:#)]
// @Required 2
// @Stable Stable
// @Stable stable
// @Short Controls a NPC's Anchor Trait.
// @Author aufdemrand

Expand Down Expand Up @@ -112,7 +112,7 @@ public void registerCoreMembers() {
// @Name Animate
// @Usage animate [<entity>|...] [animation:<name>]
// @Required 1
// @Stable Todo
// @Stable stable
// @Short Makes a list of entities perform a certain animation.
// @Author David Cernat
// @Description
Expand All @@ -129,7 +129,7 @@ public void registerCoreMembers() {
// @Name AnimateChest
// @Usage animatechest [<location>] ({open}/close) (sound:{true}/false)
// @Required 1
// @Stable Unstable
// @Stable unstable
// @Short Makes a chest open or close.
// @Author Todo
// @Description
Expand Down Expand Up @@ -180,7 +180,7 @@ public void registerCoreMembers() {
// @Name Attack
// @Usage attack (cancel) (<entity>|...) (target:<entity>)
// @Required 0
// @Stable unstable
// @Stable stable
// @Short Makes a list of entities attack a target.
// @Author David Cernat
// @Description
Expand Down Expand Up @@ -641,7 +641,7 @@ public void registerCoreMembers() {
// @Name Explode
// @Usage explode (power:<#.#>) (<location>) (fire) (breakblocks)
// @Required 0
// @Stable Todo
// @Stable stable
// @Short Causes an explosion at the location.
// @Author Alain Blanquet
// @Description
Expand Down Expand Up @@ -763,7 +763,7 @@ public void registerCoreMembers() {
// @Name Fly
// @Usage fly (cancel) [<entity>|...] (controller:<player>) (origin:<location>) (destinations:<location>|...) (speed:<#.#>) (rotationthreshold:<#.#>)
// @Required 1
// @Stable Todo
// @Stable stable
// @Short Make an entity fly where its controller is looking or fly to waypoints.
// @Author David Cernat
// @Description
Expand Down Expand Up @@ -857,7 +857,7 @@ public void registerCoreMembers() {
// @Name Head
// @Usage head (<entity>|...) [skin:<player>]
// @Required 1
// @Stable Todo
// @Stable stable
// @Short Makes players or NPCs wear a specific player's head.
// @Author David Cernat
// @Description
Expand Down Expand Up @@ -943,7 +943,7 @@ public void registerCoreMembers() {
// @Name Inventory
// @Usage inventory [open/copy/move/swap/add/remove/keep/exclude/fill/clear] (destination:<inventory>) (origin:<inventory>)
// @Required 1
// @Stable unstable
// @Stable stable
// @Short Edits the inventory of a player, NPC, or chest.
// @Author David Cernat, morphan1
// @Description
Expand Down Expand Up @@ -1083,9 +1083,9 @@ public void registerCoreMembers() {

// <--[command]
// @Name Midi
// @Usage midi [file:<name>] (<location>/<player>|...) (tempo:<#.#>)
// @Usage midi (cancel) [<file>] (<location>/<entity>|...) (tempo:<#.#>)
// @Required 1
// @Stable Stable
// @Stable stable
// @Short Plays a midi file at a given location or to a list of players using note block sounds.
// @Author David Cernat
// @Description
Expand All @@ -1104,13 +1104,13 @@ public void registerCoreMembers() {
// - midi file:Mysong <server.list_online_players>
// -->
registerCoreMember(MidiCommand.class,
"MIDI", "midi [file:<name>] (<location>/<player>|...) (tempo:<#.#>)", 1);
"MIDI", "midi (cancel) [<file>] (<location>/<entity>|...) (tempo:<#.#>)", 1);

// <--[command]
// @Name Mount
// @Usage mount (cancel) [<entity>|...] (<location>)
// @Required 0
// @Stable unstable
// @Stable stable
// @Short Mounts one entity onto another.
// @Author David Cernat
// @Description
Expand Down Expand Up @@ -1543,7 +1543,7 @@ public void registerCoreMembers() {
// @Name Sign
// @Usage sign (type:{sign_post}/wall_sign) ["<line>|..."] [<location>] (direction:n/e/w/s)
// @Required 1
// @Stable unstable
// @Stable stable
// @Short Modifies a sign.
// @Author David Cernat
// @Description
Expand Down Expand Up @@ -1578,7 +1578,7 @@ public void registerCoreMembers() {
// @Name Spawn
// @Usage spawn [<entity>|...] (<location>) (target:<entity>)
// @Required 1
// @Stable Todo
// @Stable stable
// @Short Spawns an entity.
// @Author David Cernat
// @Description
Expand Down
Expand Up @@ -10,7 +10,6 @@
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.midi.NoteBlockReceiver;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizen.utilities.debugging.dB.Messages;

Expand Down Expand Up @@ -39,7 +38,13 @@ public class MidiCommand extends AbstractCommand {
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {

for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) {
if (!scriptEntry.hasObject("location") &&

if (!scriptEntry.hasObject("cancel")
&& (arg.matches("cancel") || arg.matches("stop")))

scriptEntry.addObject("cancel", "");

else if (!scriptEntry.hasObject("location") &&
arg.matchesArgumentType(dLocation.class))

scriptEntry.addObject("location", arg.asType(dLocation.class));
Expand Down Expand Up @@ -68,8 +73,10 @@ else if (!scriptEntry.hasObject("file")) {
else throw new InvalidArgumentsException(Messages.ERROR_UNKNOWN_ARGUMENT, arg.raw_value);
}

// Check required args
if (!scriptEntry.hasObject("file"))
// Produce error if there is no file and the "cancel" argument was
// not used
if (!scriptEntry.hasObject("file")
&& !scriptEntry.hasObject("cancel"))
throw new InvalidArgumentsException(Messages.ERROR_MISSING_OTHER, "FILE");

if (!scriptEntry.hasObject("location")) {
Expand All @@ -84,9 +91,10 @@ else if (!scriptEntry.hasObject("file")) {
@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {

File file = new File(scriptEntry.getElement("file").asString());
boolean cancel = scriptEntry.hasObject("cancel");
File file = !cancel ? new File(scriptEntry.getElement("file").asString()) : null;

if (!file.exists()) {
if (!cancel && !file.exists()) {
dB.echoError("Invalid file " + scriptEntry.getElement("file").asString());
return;
}
Expand All @@ -96,17 +104,28 @@ public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
float tempo = (float) scriptEntry.getElement("tempo").asDouble();

// Report to dB
dB.report(getName(), aH.debugObj("file", file.getPath()) +
dB.report(getName(), (cancel == true ? 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));

// Play the midi
if (location != null) {
MidiUtil.playMidi(file, tempo, location);
if (cancel == false) {
if (location != null) {
MidiUtil.playMidi(file, tempo, location);
}
else {
MidiUtil.playMidi(file, tempo, entities);
}
}
else {
MidiUtil.playMidi(file, tempo, entities);
if (location != null) {
MidiUtil.stopMidi(location.identify());
}
else {
MidiUtil.stopMidi(entities);
}
}
}
}
Expand Up @@ -78,6 +78,12 @@ public static void stopMidi(String object) {
}
}

public static void stopMidi(List<dEntity> entities) {
for (dEntity entity : entities) {
stopMidi(entity.identify());
}
}

// provided by github.com/sk89q/craftbook
private static final int[] instruments = {
0, 0, 0, 0, 0, 0, 0, 5, // 8
Expand Down

0 comments on commit 9ccbcfe

Please sign in to comment.