Skip to content

Commit

Permalink
add player-targeting to playsound
Browse files Browse the repository at this point in the history
- playsound note_piano <player> === play a sound effect for only that
player to hear, nobody nearby will hear it. Also accepts a list of
players. Location-based commands will still work 100%.
  • Loading branch information
mcmonkey4eva committed Nov 2, 2013
1 parent ecfacb1 commit 1a3b2a6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
Expand Up @@ -1540,10 +1540,10 @@ public void registerCoreMembers() {

// <--[command]
// @Name PlaySound
// @Syntax playsound [<location>] [sound:<name>] (volume:<#.#>) (pitch:<#.#>)
// @Syntax playsound [<location>/<player>|...] [sound:<name>] (volume:<#.#>) (pitch:<#.#>)
// @Required 2
// @Stable stable
// @Short Plays a sound at the location.
// @Short Plays a sound at the location or to a list of players.
// @Author Todo
// @Description
// Todo
Expand All @@ -1553,7 +1553,7 @@ public void registerCoreMembers() {
// Todo
// -->
registerCoreMember(PlaySoundCommand.class,
"PLAYSOUND", "playsound [<location>] [sound:<name>] (volume:<#.#>) (pitch:<#.#>)", 2);
"PLAYSOUND", "playsound [<location>/<player>|...] [sound:<name>] (volume:<#.#>) (pitch:<#.#>)", 2);


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

import net.aufdemrand.denizen.objects.*;
import org.bukkit.Sound;

import net.aufdemrand.denizen.exceptions.CommandExecutionException;
import net.aufdemrand.denizen.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizen.scripts.ScriptEntry;
import net.aufdemrand.denizen.scripts.commands.AbstractCommand;
import net.aufdemrand.denizen.objects.Element;
import net.aufdemrand.denizen.objects.aH;
import net.aufdemrand.denizen.objects.dLocation;
import net.aufdemrand.denizen.utilities.debugging.dB;

import java.util.List;

/* PLAYSOUND [LOCATION:x,y,z,world] [SOUND:NAME] (VOLUME:#) (PITCH:#)*/

/*
Expand All @@ -35,9 +35,15 @@ public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException
for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) {

if (!scriptEntry.hasObject("location")
&& !scriptEntry.hasObject("entities")
&& arg.matchesArgumentType(dLocation.class))
scriptEntry.addObject("location", arg.asType(dLocation.class));

else if (!scriptEntry.hasObject("location")
&& !scriptEntry.hasObject("entities")
&& arg.matchesArgumentList(dPlayer.class))
scriptEntry.addObject("entities", ((dList)arg.asType(dList.class)).filter(dPlayer.class));

else if (!scriptEntry.hasObject("volume")
&& arg.matchesPrimitive(aH.PrimitiveType.Double)
&& arg.matchesPrefix("volume, v"))
Expand All @@ -60,30 +66,38 @@ else if (!scriptEntry.hasObject("sound")

if (!scriptEntry.hasObject("sound"))
throw new InvalidArgumentsException("Missing sound argument!");
if (!scriptEntry.hasObject("location"))
if (!scriptEntry.hasObject("location") && !scriptEntry.hasObject("entities"))
throw new InvalidArgumentsException("Missing location argument!");

scriptEntry.defaultObject("volume", new Element(1));
scriptEntry.defaultObject("pitch", new Element(1));

}

@SuppressWarnings("unchecked")
@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {

dLocation location = (dLocation) scriptEntry.getObject("location");
List<dPlayer> players = (List<dPlayer>) scriptEntry.getObject("entities");
Element sound = scriptEntry.getElement("sound");
Element volume = scriptEntry.getElement("volume");
Element pitch = scriptEntry.getElement("pitch");

dB.report(scriptEntry, getName(),
location.debug() +
(location != null ? location.debug(): "") +
(players != null ? aH.debugObj("entities", players.toString()): "") +
sound.debug() +
volume.debug() +
pitch.debug());

try {
location.getWorld().playSound(location, Sound.valueOf(sound.asString().toUpperCase()), volume.asFloat(), pitch.asFloat());
if (location != null) location.getWorld().playSound(location, Sound.valueOf(sound.asString().toUpperCase()), volume.asFloat(), pitch.asFloat());
else {
for (dPlayer player: players) {
player.getPlayerEntity().playSound(player.getLocation(), Sound.valueOf(sound.asString().toUpperCase()), volume.asFloat(), pitch.asFloat());
}
}
} catch (Exception e) {
dB.echoError("Invalid sound!");
}
Expand Down

0 comments on commit 1a3b2a6

Please sign in to comment.