diff --git a/src/main/java/net/aufdemrand/denizen/scripts/commands/CommandRegistry.java b/src/main/java/net/aufdemrand/denizen/scripts/commands/CommandRegistry.java index 38a21dabed..8f6d6f258d 100644 --- a/src/main/java/net/aufdemrand/denizen/scripts/commands/CommandRegistry.java +++ b/src/main/java/net/aufdemrand/denizen/scripts/commands/CommandRegistry.java @@ -1144,22 +1144,30 @@ public void registerCoreMembers() { // <--[command] // @Name Midi - // @Usage midi [file:] [/listeners:|...] (tempo:<#.#>) + // @Usage midi [file:] (/|...) (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 + // + // @Usage + // Use to play a midi song file at a given location to the specified player + // - midi file:Mysong // @Example // Todo // --> registerCoreMember(MidiCommand.class, - "MIDI", "midi [file:] [/listeners:|...] (tempo:<#.#>)", 1); + "MIDI", "midi [file:] (/|...) (tempo:<#.#>)", 1); // <--[command] // @Name Mount diff --git a/src/main/java/net/aufdemrand/denizen/scripts/commands/world/MidiCommand.java b/src/main/java/net/aufdemrand/denizen/scripts/commands/world/MidiCommand.java index 6a4ae2a254..12ca7395a6 100644 --- a/src/main/java/net/aufdemrand/denizen/scripts/commands/world/MidiCommand.java +++ b/src/main/java/net/aufdemrand/denizen/scripts/commands/world/MidiCommand.java @@ -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; @@ -10,8 +12,7 @@ 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; @@ -19,7 +20,6 @@ import net.aufdemrand.denizen.utilities.debugging.dB; import net.aufdemrand.denizen.utilities.debugging.dB.Messages; -/* midi [file:] (listener(s):[p@|...])|(location:) (tempo:<#.#>) */ /** * Arguments: [] - Required, () - Optional @@ -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 listeners = new HashSet(); + 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 listeners = (Set) 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 listenerSet = new HashSet(); + for (String player: listeners.toArray()) { + listenerSet.add(dPlayer.valueOf(player).getPlayerEntity()); + } + MidiUtil.playMidiQuietly(file, tempo, listenerSet); } } } diff --git a/src/main/java/net/aufdemrand/denizen/scripts/containers/core/WorldScriptHelper.java b/src/main/java/net/aufdemrand/denizen/scripts/containers/core/WorldScriptHelper.java index 52dbd66740..ea37b0e683 100644 --- a/src/main/java/net/aufdemrand/denizen/scripts/containers/core/WorldScriptHelper.java +++ b/src/main/java/net/aufdemrand/denizen/scripts/containers/core/WorldScriptHelper.java @@ -352,7 +352,7 @@ public void blockDamage(BlockDamageEvent event) { if (determination.toUpperCase().startsWith("INSTABREAK")) event.setInstaBreak(true); } - + // <--[event] // @Events // block fades @@ -369,21 +369,21 @@ public void blockDamage(BlockDamageEvent event) { // --> @EventHandler public void blockFade(BlockFadeEvent event) { - + Map context = new HashMap(); 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]