Skip to content
This repository has been archived by the owner on Apr 12, 2022. It is now read-only.

Commit

Permalink
Custom Sounds for PlaySound
Browse files Browse the repository at this point in the history
  • Loading branch information
Xenmai committed Oct 14, 2017
1 parent 9ba07e7 commit 28c2424
Showing 1 changed file with 32 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.denizenscript.denizen2core.commands.AbstractCommand;
import com.denizenscript.denizen2core.commands.CommandEntry;
import com.denizenscript.denizen2core.commands.CommandQueue;
import com.denizenscript.denizen2core.tags.objects.BooleanTag;
import com.denizenscript.denizen2core.tags.objects.NumberTag;
import com.denizenscript.denizen2core.utilities.debugging.ColorSet;
import com.denizenscript.denizen2sponge.tags.objects.LocationTag;
Expand All @@ -14,13 +15,13 @@
public class PlaySoundCommand extends AbstractCommand {

// <--[explanation]
// @Name Sound Types
// @Name Default Sound Types
// @Group Useful Lists
// @Description
// A list of all default sound types can be found here:
// <@link url https://jd.spongepowered.org/7.0.0-SNAPSHOT/org/spongepowered/api/effect/sound/SoundTypes.html>sound types list<@/link>
// <@link url https://jd.spongepowered.org/7.0.0-SNAPSHOT/org/spongepowered/api/effect/sound/SoundTypes.html>default sound types list<@/link>
// These can be used with the playsound command, if you replace '_' with '.'.
// For example, 'entity.arrow.hit' is a valid sound type.
// For example, 'entity.arrow.hit' is a valid default sound type.
// -->

// <--[command]
Expand All @@ -29,17 +30,23 @@ public class PlaySoundCommand extends AbstractCommand {
// @Short plays a sound.
// @Updated 2017/04/24
// @Group World
// @Minimum 2
// @Maximum 2
// @Minimum 3
// @Maximum 3
// @Named pitch (NumberTag) Sets the pitch of the sound.
// @Named min_volume (NumberTag) Sets the minimum volume of the sound.
// @Named custom (BooleanTag) Sets whether the sound is custom.
// @Description
// Plays a sound at certain location. Optionally specify a pitch and minimum volume.
// Related information: <@link explanation Sound Types>sound types<@/link>.
// The custom argument will allow any input without error, regardless of whether
// players have the given ID in their resource pack.
// Related information: <@link explanation Sound Types>default sound types<@/link>.
// TODO: Explain more!
// @Example
// # This example plays a loud 'entity_arrow_hit' sound at the player's location.
// # This example plays a loud 'entity_arrow_hit' default sound at the player's location.
// - playsound <player.location> entity.arrow.hit 2
// @Example
// # This example plays a loud 'pirate_cannon' custom sound at the player's location.
// - playsound <player.location> pirate_cannon 3 --custom true
// -->

@Override
Expand All @@ -66,43 +73,50 @@ public int getMaximumArguments() {
public void execute(CommandQueue queue, CommandEntry entry) {
LocationTag loc = LocationTag.getFor(queue.error, entry.getArgumentObject(queue, 0));
String soundName = entry.getArgumentObject(queue, 1).toString();
Optional<SoundType> type = Sponge.getRegistry().getType(SoundType.class, soundName);
if (!type.isPresent()) {
queue.handleError(entry, "Invalid sound type: '" + soundName + "'!");
return;
SoundType sound;
if (entry.namedArgs.containsKey("custom") &&
BooleanTag.getFor(queue.error, entry.getNamedArgumentObject(queue, "custom")).getInternal()) {
sound = SoundType.of(soundName);
}
else {
Optional<SoundType> opt = Sponge.getRegistry().getType(SoundType.class, soundName);
if (!opt.isPresent()) {
queue.handleError(entry, "Vanilla sound type not found: '" + soundName + "'!");
return;
}
sound = opt.get();
}
NumberTag volume = NumberTag.getFor(queue.error, entry.getArgumentObject(queue, 2));
NumberTag pitch;
NumberTag min_volume;
// TODO: Only play the sound to a list of target players.
// TODO: Allow for playing custom sounds.
if (entry.namedArgs.containsKey("pitch")) {
pitch = NumberTag.getFor(queue.error, entry.getNamedArgumentObject(queue, "pitch"));
if (entry.namedArgs.containsKey("min_volume")) {
min_volume = NumberTag.getFor(queue.error, entry.getNamedArgumentObject(queue, "min_volume"));
loc.getInternal().world.playSound(type.get(), loc.getInternal().toVector3d(), volume.getInternal(), pitch.getInternal(), min_volume.getInternal());
loc.getInternal().world.playSound(sound, loc.getInternal().toVector3d(), volume.getInternal(), pitch.getInternal(), min_volume.getInternal());
if (queue.shouldShowGood()) {
queue.outGood("Successfully played the sound of type '" + ColorSet.emphasis + type.get().getId() +
queue.outGood("Successfully played the sound of type '" + ColorSet.emphasis + sound.getId() +
ColorSet.good + "' with volume of " + ColorSet.emphasis + volume.debug() + ColorSet.good +
", pitch of " + ColorSet.emphasis + pitch.debug() + ColorSet.good + " and minimum volume of " +
ColorSet.emphasis + min_volume.debug() + ColorSet.good + " at location " + ColorSet.emphasis +
loc.debug() + ColorSet.good + "!");
}
}
else {
loc.getInternal().world.playSound(type.get(), loc.getInternal().toVector3d(), volume.getInternal(), pitch.getInternal());
loc.getInternal().world.playSound(sound, loc.getInternal().toVector3d(), volume.getInternal(), pitch.getInternal());
if (queue.shouldShowGood()) {
queue.outGood("Successfully played the sound of type '" + ColorSet.emphasis + type.get().getId() +
queue.outGood("Successfully played the sound of type '" + ColorSet.emphasis + sound.getId() +
ColorSet.good + "' with volume of " + ColorSet.emphasis + volume.debug() + ColorSet.good +
" and pitch of " + ColorSet.emphasis + pitch.debug() + ColorSet.good + " at location " +
ColorSet.emphasis + loc.debug() + ColorSet.good + "!");
}
}
}
else {
loc.getInternal().world.playSound(type.get(), loc.getInternal().toVector3d(), volume.getInternal());
loc.getInternal().world.playSound(sound, loc.getInternal().toVector3d(), volume.getInternal());
if (queue.shouldShowGood()) {
queue.outGood("Successfully played the sound of type '" + ColorSet.emphasis + type.get().getId() +
queue.outGood("Successfully played the sound of type '" + ColorSet.emphasis + sound.getId() +
ColorSet.good + "' with volume of " + ColorSet.emphasis + volume.debug() + ColorSet.good +
" at location " + ColorSet.emphasis + loc.debug() + ColorSet.good + "!");
}
Expand Down

0 comments on commit 28c2424

Please sign in to comment.