Skip to content

Commit

Permalink
fix playsound and midi arg parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Nov 28, 2021
1 parent 93a1ea1 commit 051f7c0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 53 deletions.
Expand Up @@ -68,7 +68,7 @@ public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException
for (Argument arg : scriptEntry) {
if (!scriptEntry.hasObject("cancel")
&& (arg.matches("cancel") || arg.matches("stop"))) {
scriptEntry.addObject("cancel", "");
scriptEntry.addObject("cancel", "true");
}
else if (!scriptEntry.hasObject("location") &&
arg.matchesArgumentType(LocationTag.class)) {
Expand All @@ -88,15 +88,7 @@ else if (!scriptEntry.hasObject("tempo") &&
scriptEntry.addObject("tempo", arg.asElement());
}
else if (!scriptEntry.hasObject("file")) {

String path = Denizen.getInstance().getDataFolder() +
File.separator + "midi" +
File.separator + arg.getValue();
if (!path.endsWith(".mid")) {
path = path + ".mid";
}

scriptEntry.addObject("file", new ElementTag(path));
scriptEntry.addObject("file", arg.asElement());
}
else {
arg.reportUnhandled();
Expand All @@ -115,24 +107,29 @@ else if (!scriptEntry.hasObject("file")) {
@Override
public void execute(final ScriptEntry scriptEntry) {
boolean cancel = scriptEntry.hasObject("cancel");
File file = !cancel ? new File(scriptEntry.getElement("file").asString()) : null;
if (!cancel && !Utilities.canReadFile(file)) {
Debug.echoError("Cannot read from that file path due to security settings in Denizen/config.yml.");
return;
}
if (!cancel && !file.exists()) {
Debug.echoError(scriptEntry.getResidingQueue(), "Invalid file " + scriptEntry.getElement("file").asString());
return;
}
ElementTag filePath = scriptEntry.getElement("file");
List<EntityTag> entities = (List<EntityTag>) scriptEntry.getObject("entities");
LocationTag location = scriptEntry.getObjectTag("location");
float tempo = scriptEntry.getElement("tempo").asFloat();
float volume = scriptEntry.getElement("volume").asFloat();
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), (cancel ? db("cancel", true) : ""), db("file", file.getPath()), db("entities", entities), location, db("tempo", tempo), db("volume", volume));
Debug.report(scriptEntry, getName(), (cancel ? db("cancel", true) : ""), filePath, db("entities", entities), location, db("tempo", tempo), db("volume", volume));
}
// Play the midi
if (!cancel) {
String fName = scriptEntry.getElement("file").asString();
if (!fName.endsWith(".mid")) {
fName += ".mid";
}
File file = new File(Denizen.getInstance().getDataFolder(), "/midi/" + fName);
if (!Utilities.canReadFile(file)) {
Debug.echoError("Cannot read from that file path due to security settings in Denizen/config.yml.");
return;
}
if (!file.exists()) {
Debug.echoError(scriptEntry.getResidingQueue(), "Invalid file " + filePath.asString());
return;
}
NoteBlockReceiver rec;
if (location != null) {
rec = MidiUtil.playMidi(file, tempo, volume, location);
Expand Down
Expand Up @@ -19,14 +19,16 @@ public class PlaySoundCommand extends AbstractCommand {

public PlaySoundCommand() {
setName("playsound");
setSyntax("playsound (<location>|...) (<player>|...) [sound:<name>] (volume:<#.#>) (pitch:<#.#>) (custom) (sound_category:<category name>)");
setSyntax("playsound (<location>|...) (<player>|...) [sound:<name>] (volume:<#.#>) (pitch:<#.#>) (custom) (sound_category:<category_name>)");
setRequiredArguments(2, 7);
isProcedural = false;
setBooleansHandled("custom");
setPrefixesHandled("sound_category", "pitch", "volume");
}

// <--[command]
// @Name PlaySound
// @Syntax playsound (<location>|...) (<player>|...) [sound:<name>] (volume:<#.#>) (pitch:<#.#>) (custom) (sound_category:<category name>)
// @Syntax playsound (<location>|...) (<player>|...) [sound:<name>] (volume:<#.#>) (pitch:<#.#>) (custom) (sound_category:<category_name>)
// @Required 2
// @Maximum 7
// @Short Plays a sound at the location or to a list of players.
Expand Down Expand Up @@ -88,27 +90,10 @@ else if (!scriptEntry.hasObject("entities")
&& arg.matchesArgumentList(PlayerTag.class)) {
scriptEntry.addObject("entities", arg.asType(ListTag.class).filter(PlayerTag.class, scriptEntry));
}
else if (!scriptEntry.hasObject("volume")
&& arg.matchesFloat()
&& arg.matchesPrefix("volume", "v")) {
scriptEntry.addObject("volume", arg.asElement());
}
else if (!scriptEntry.hasObject("pitch")
&& arg.matchesFloat()
&& arg.matchesPrefix("pitch", "p")) {
scriptEntry.addObject("pitch", arg.asElement());
}
else if (!scriptEntry.hasObject("sound")) {
else if (!scriptEntry.hasObject("sound")
&& arg.limitToOnlyPrefix("sound")) {
scriptEntry.addObject("sound", arg.asElement());
}
else if (!scriptEntry.hasObject("custom")
&& arg.matches("custom")) {
scriptEntry.addObject("custom", new ElementTag(true));
}
else if (!scriptEntry.hasObject("sound_category")
&& arg.matchesPrefix("sound_category")) {
scriptEntry.addObject("sound_category", arg.asElement());
}
else {
arg.reportUnhandled();
}
Expand All @@ -119,31 +104,27 @@ else if (!scriptEntry.hasObject("sound_category")
if (!scriptEntry.hasObject("locations") && !scriptEntry.hasObject("entities")) {
throw new InvalidArgumentsException("Missing location argument!");
}
scriptEntry.defaultObject("volume", new ElementTag(1));
scriptEntry.defaultObject("pitch", new ElementTag(1));
scriptEntry.defaultObject("custom", new ElementTag(false));
scriptEntry.defaultObject("sound_category", new ElementTag("MASTER"));
}

@Override
public void execute(ScriptEntry scriptEntry) {
List<LocationTag> locations = (List<LocationTag>) scriptEntry.getObject("locations");
List<PlayerTag> players = (List<PlayerTag>) scriptEntry.getObject("entities");
ElementTag soundElement = scriptEntry.getElement("sound");
ElementTag volumeElement = scriptEntry.getElement("volume");
ElementTag pitchElement = scriptEntry.getElement("pitch");
ElementTag custom = scriptEntry.getElement("custom");
ElementTag sound_category = scriptEntry.getElement("sound_category");
ElementTag volumeElement = scriptEntry.argForPrefixAsElement("volume", "1");
ElementTag pitchElement = scriptEntry.argForPrefixAsElement("pitch", "1");
boolean custom = scriptEntry.argAsBoolean("custom");
ElementTag sound_category = scriptEntry.argForPrefixAsElement("sound_category", "MASTER");
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), db("locations", locations), db("entities", players), soundElement, volumeElement, pitchElement, custom);
Debug.report(scriptEntry, getName(), db("locations", locations), db("entities", players), soundElement, volumeElement, pitchElement, db("custom", custom));
}
String sound = soundElement.asString();
float volume = volumeElement.asFloat();
float pitch = pitchElement.asFloat();
String category = sound_category.asString().toUpperCase();
try {
if (players == null) {
if (custom.asBoolean()) {
if (custom) {
for (LocationTag location : locations) {
NMSHandler.getSoundHelper().playSound(null, location, sound, volume, pitch, category);
}
Expand All @@ -157,7 +138,7 @@ public void execute(ScriptEntry scriptEntry) {
else if (locations != null) {
for (LocationTag location : locations) {
for (PlayerTag player : players) {
if (custom.asBoolean()) {
if (custom) {
NMSHandler.getSoundHelper().playSound(player.getPlayerEntity(), location, sound, volume, pitch, category);
}
else {
Expand All @@ -168,7 +149,7 @@ else if (locations != null) {
}
else {
for (PlayerTag player : players) {
if (custom.asBoolean()) {
if (custom) {
NMSHandler.getSoundHelper().playSound(player.getPlayerEntity(), player.getLocation(), sound, volume, pitch, category);
}
else {
Expand Down

0 comments on commit 051f7c0

Please sign in to comment.