Skip to content

Commit

Permalink
playsound: allow player + location arguments simultaneously
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Jan 21, 2021
1 parent 0e4df60 commit f933d43
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 20 deletions.
Expand Up @@ -57,6 +57,8 @@ public AdvancementCommand() {
// The hidden argument sets whether the advancement should be hidden until it is completed.
// The x and y arguments are offsets based on the size of an advancement icon in the menu. They are required for custom tabs to look reasonable.
//
// To award a pre-existing vanilla advancement, instead use <@link mechanism PlayerTag.award_advancement>
//
// WARNING: Failure to re-create advancements on every server start may result in loss of data - use <@link event server prestart>.
//
// @Tags
Expand Down
Expand Up @@ -136,7 +136,9 @@ else if (!scriptEntry.hasObject("script")) {
if (!script.isValid()) {
arg.reportUnhandled();
}
scriptEntry.addObject("script", script);
else {
scriptEntry.addObject("script", script);
}
}
else {
arg.reportUnhandled();
Expand Down Expand Up @@ -181,4 +183,3 @@ public void execute(ScriptEntry scriptEntry) {
scriptEntry.addObject("command", new ElementTag("/denizenclickable " + id));
}
}

Expand Up @@ -19,16 +19,16 @@ public class PlaySoundCommand extends AbstractCommand {

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

// <--[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 6
// @Maximum 7
// @Short Plays a sound at the location or to a list of players.
// @Group world
//
Expand All @@ -44,6 +44,7 @@ public PlaySoundCommand() {
//
// Specifying a player or list of players will only play the sound for each player, from their own location (but will not follow them if they move).
// If a location is specified, it will play the sound for any players that are near the location specified.
// If both players and locations are specified, will play the sound for only those players at those locations.
//
// Optionally, specify 'custom' to play a custom sound added by a resource pack, changing the sound name to something like 'random.click'
//
Expand All @@ -68,16 +69,12 @@ public PlaySoundCommand() {

@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {

for (Argument arg : scriptEntry.getProcessedArgs()) {

if (!scriptEntry.hasObject("locations")
&& !scriptEntry.hasObject("entities")
&& arg.matchesArgumentList(LocationTag.class)) {
scriptEntry.addObject("locations", arg.asType(ListTag.class).filter(LocationTag.class, scriptEntry));
}
else if (!scriptEntry.hasObject("locations")
&& !scriptEntry.hasObject("entities")
else if (!scriptEntry.hasObject("entities")
&& arg.matchesArgumentList(PlayerTag.class)) {
scriptEntry.addObject("entities", arg.asType(ListTag.class).filter(PlayerTag.class, scriptEntry));
}
Expand Down Expand Up @@ -105,35 +102,29 @@ else if (!scriptEntry.hasObject("sound_category")
else {
arg.reportUnhandled();
}

}

if (!scriptEntry.hasObject("sound")) {
throw new InvalidArgumentsException("Missing sound argument!");
}
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"));

}

@SuppressWarnings("unchecked")
@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");

if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(),
(locations != null ? ArgumentHelper.debugObj("locations", locations.toString()) : "") +
Expand All @@ -143,14 +134,12 @@ public void execute(ScriptEntry scriptEntry) {
pitchElement.debug() +
custom.debug());
}

String sound = soundElement.asString();
float volume = volumeElement.asFloat();
float pitch = pitchElement.asFloat();
String category = sound_category.asString().toUpperCase();

try {
if (locations != null) {
if (players == null) {
if (custom.asBoolean()) {
for (LocationTag location : locations) {
NMSHandler.getSoundHelper().playSound(null, location, sound, volume, pitch, category);
Expand All @@ -162,6 +151,18 @@ public void execute(ScriptEntry scriptEntry) {
}
}
}
else if (locations != null) {
for (LocationTag location : locations) {
for (PlayerTag player : players) {
if (custom.asBoolean()) {
NMSHandler.getSoundHelper().playSound(player.getPlayerEntity(), location, sound, volume, pitch, category);
}
else {
NMSHandler.getSoundHelper().playSound(player.getPlayerEntity(), location, Sound.valueOf(sound.toUpperCase()), volume, pitch, category);
}
}
}
}
else {
for (PlayerTag player : players) {
if (custom.asBoolean()) {
Expand Down

0 comments on commit f933d43

Please sign in to comment.