|
| 1 | +/* |
| 2 | + * This file is part of Skript. |
| 3 | + * |
| 4 | + * Skript is free software: you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU General Public License as published by |
| 6 | + * the Free Software Foundation, either version 3 of the License, or |
| 7 | + * (at your option) any later version. |
| 8 | + * |
| 9 | + * Skript is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License |
| 15 | + * along with Skript. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + * |
| 17 | + * |
| 18 | + * Copyright 2011-2016 Peter Güttinger and contributors |
| 19 | + * |
| 20 | + */ |
| 21 | + |
| 22 | +package ch.njol.skript.effects; |
| 23 | + |
| 24 | +import org.bukkit.Location; |
| 25 | +import org.bukkit.SoundCategory; |
| 26 | +import org.bukkit.entity.Player; |
| 27 | +import org.bukkit.event.Event; |
| 28 | +import org.eclipse.jdt.annotation.Nullable; |
| 29 | + |
| 30 | +import ch.njol.skript.Skript; |
| 31 | +import ch.njol.skript.doc.Description; |
| 32 | +import ch.njol.skript.doc.Examples; |
| 33 | +import ch.njol.skript.doc.Name; |
| 34 | +import ch.njol.skript.doc.Since; |
| 35 | +import ch.njol.skript.lang.Effect; |
| 36 | +import ch.njol.skript.lang.Expression; |
| 37 | +import ch.njol.skript.lang.SkriptParser.ParseResult; |
| 38 | +import ch.njol.util.Kleenean; |
| 39 | + |
| 40 | +@Name("Play Sound") |
| 41 | +@Description("Plays a sound at given location for everyone or just for given players. Playing sounds from resource packs is supported.") |
| 42 | +@Examples("") |
| 43 | +@Since("2.2-dev28") |
| 44 | +public class EffPlaySound extends Effect { |
| 45 | + |
| 46 | + static { |
| 47 | + Skript.registerEffect(EffPlaySound.class, "play sound %string% [with volume %number%] [(and|with) pitch %number%] at %location% [for %players%]"); |
| 48 | + } |
| 49 | + |
| 50 | + @SuppressWarnings("null") |
| 51 | + private Expression<String> sound; |
| 52 | + @Nullable |
| 53 | + private Expression<Number> volume; |
| 54 | + @Nullable |
| 55 | + private Expression<Number> pitch; |
| 56 | + |
| 57 | + @SuppressWarnings("null") |
| 58 | + private Expression<Location> location; |
| 59 | + @Nullable |
| 60 | + private Expression<Player> players; |
| 61 | + |
| 62 | + @SuppressWarnings({"unchecked", "null"}) |
| 63 | + @Override |
| 64 | + public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { |
| 65 | + sound = (Expression<String>) exprs[0]; |
| 66 | + volume = (Expression<Number>) exprs[1]; |
| 67 | + pitch = (Expression<Number>) exprs[2]; |
| 68 | + location = (Expression<Location>) exprs[3]; |
| 69 | + players = (Expression<Player>) exprs[4]; |
| 70 | + |
| 71 | + return true; |
| 72 | + } |
| 73 | + |
| 74 | + @SuppressWarnings("null") |
| 75 | + @Override |
| 76 | + protected void execute(Event e) { |
| 77 | + Location l = location.getSingle(e); |
| 78 | + |
| 79 | + String s = sound.getSingle(e); |
| 80 | + float vol = volume != null ? volume.getSingle(e).floatValue() : 0; |
| 81 | + float pi = pitch != null ? pitch.getSingle(e).floatValue() : 0; |
| 82 | + |
| 83 | + if (players != null) { |
| 84 | + for (Player p : players.getAll(e)) { |
| 85 | + p.playSound(l, s, SoundCategory.MASTER, vol, pi); |
| 86 | + } |
| 87 | + } else { |
| 88 | + l.getWorld().playSound(l, s, vol, pi); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public String toString(@Nullable Event e, boolean debug) { |
| 94 | + if (e != null) |
| 95 | + return "play sound " + sound.getSingle(e); |
| 96 | + return "play sound"; |
| 97 | + } |
| 98 | + |
| 99 | +} |
0 commit comments