-
-
Notifications
You must be signed in to change notification settings - Fork 382
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor EffPlaySound to be more readable and resistant to versioning…
… issues. (#7022) * save java 17 version for later * Java 11 version * Update src/main/java/ch/njol/skript/effects/EffPlaySound.java Co-authored-by: Asleepp <119438940+Asleeepp@users.noreply.github.com> * fix legacy versions * local tests are being weird, trying actions * cringe utils fixes NCDef ? * collapse receivers * spacing --------- Co-authored-by: Asleepp <119438940+Asleeepp@users.noreply.github.com> Co-authored-by: Moderocky <admin@moderocky.com>
- Loading branch information
1 parent
3843781
commit e0862a8
Showing
6 changed files
with
307 additions
and
233 deletions.
There are no files selected for viewing
108 changes: 0 additions & 108 deletions
108
src/main/java/ch/njol/skript/bukkitutil/AdventureSoundReceiver.java
This file was deleted.
Oops, something went wrong.
57 changes: 57 additions & 0 deletions
57
src/main/java/ch/njol/skript/bukkitutil/sounds/AdventureSoundUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package ch.njol.skript.bukkitutil.sounds; | ||
|
||
import net.kyori.adventure.sound.Sound; | ||
import org.bukkit.Location; | ||
import org.bukkit.NamespacedKey; | ||
import org.bukkit.SoundCategory; | ||
import org.bukkit.World; | ||
import org.bukkit.entity.Entity; | ||
import org.bukkit.entity.Player; | ||
|
||
import java.util.OptionalLong; | ||
|
||
public class AdventureSoundUtils { | ||
|
||
public static Sound getAdventureSound(NamespacedKey key, SoundCategory category, float volume, float pitch, OptionalLong seed) { | ||
return Sound.sound() | ||
.source(category) | ||
.volume(volume) | ||
.pitch(pitch) | ||
.seed(seed) | ||
.type(key) | ||
.build(); | ||
} | ||
|
||
public static void playSound(World world, Location location, NamespacedKey sound, SoundCategory category, float volume, float pitch, OptionalLong seed) { | ||
world.playSound( | ||
AdventureSoundUtils.getAdventureSound(sound, category, volume, pitch, seed), | ||
location.x(), | ||
location.y(), | ||
location.z() | ||
); | ||
} | ||
|
||
public static void playSound(World world, Entity entity, NamespacedKey sound, SoundCategory category, float volume, float pitch, OptionalLong seed) { | ||
world.playSound( | ||
AdventureSoundUtils.getAdventureSound(sound, category, volume, pitch, seed), | ||
entity | ||
); | ||
} | ||
|
||
public static void playSound(Player player, Location location, NamespacedKey sound, SoundCategory category, float volume, float pitch, OptionalLong seed) { | ||
player.playSound( | ||
AdventureSoundUtils.getAdventureSound(sound, category, volume, pitch, seed), | ||
location.x(), | ||
location.y(), | ||
location.z() | ||
); | ||
} | ||
|
||
public static void playSound(Player player, Entity entity, NamespacedKey sound, SoundCategory category, float volume, float pitch, OptionalLong seed) { | ||
player.playSound( | ||
AdventureSoundUtils.getAdventureSound(sound, category, volume, pitch, seed), | ||
entity | ||
); | ||
} | ||
|
||
} |
134 changes: 134 additions & 0 deletions
134
src/main/java/ch/njol/skript/bukkitutil/sounds/SoundReceiver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
package ch.njol.skript.bukkitutil.sounds; | ||
|
||
import ch.njol.skript.Skript; | ||
import org.bukkit.Location; | ||
import org.bukkit.NamespacedKey; | ||
import org.bukkit.Sound; | ||
import org.bukkit.SoundCategory; | ||
import org.bukkit.World; | ||
import org.bukkit.entity.Entity; | ||
import org.bukkit.entity.Player; | ||
|
||
import java.util.Locale; | ||
import java.util.OptionalLong; | ||
|
||
/** | ||
* Adapter pattern to unify {@link World} and {@link Player} playSound methods. | ||
* Methods can be called without determining version support, it is handled internally. | ||
* Non-supported methods will simply delegate to supported methods. | ||
*/ | ||
public interface SoundReceiver { | ||
|
||
boolean ADVENTURE_API = Skript.classExists("net.kyori.adventure.sound.Sound$Builder"); | ||
boolean SPIGOT_SOUND_SEED = Skript.methodExists(Player.class, "playSound", Entity.class, Sound.class, SoundCategory.class, float.class, float.class, long.class); | ||
boolean ENTITY_EMITTER_SOUND = Skript.methodExists(Player.class, "playSound", Entity.class, Sound.class, SoundCategory.class, float.class, float.class); | ||
boolean ENTITY_EMITTER_STRING = Skript.methodExists(Player.class, "playSound", Entity.class, String.class, SoundCategory.class, float.class, float.class); | ||
|
||
void playSound(Location location, NamespacedKey sound, SoundCategory category, float volume, float pitch, OptionalLong seed); | ||
void playSound(Entity entity, NamespacedKey sound, SoundCategory category, float volume, float pitch, OptionalLong seed); | ||
|
||
static SoundReceiver of(Player player) { return new PlayerSoundReceiver(player); } | ||
static SoundReceiver of(World world) { return new WorldSoundReceiver(world); } | ||
|
||
// Player adapter pattern | ||
class PlayerSoundReceiver implements SoundReceiver { | ||
|
||
private final Player player; | ||
|
||
protected PlayerSoundReceiver(Player player) { | ||
this.player = player; | ||
} | ||
|
||
@Override | ||
public void playSound(Location location, NamespacedKey sound, SoundCategory category, float volume, float pitch, OptionalLong seed) { | ||
//noinspection DuplicatedCode | ||
if (ADVENTURE_API) { | ||
AdventureSoundUtils.playSound(player, location, sound, category, volume, pitch, seed); | ||
} else if (!SPIGOT_SOUND_SEED || seed.isEmpty()) { | ||
player.playSound(location, sound.getKey(), category, volume, pitch); | ||
} else { | ||
player.playSound(location, sound.getKey(), category, volume, pitch, seed.getAsLong()); | ||
} | ||
} | ||
|
||
private void playSound(Entity entity, String sound, SoundCategory category, float volume, float pitch) { | ||
//noinspection DuplicatedCode | ||
if (ENTITY_EMITTER_STRING) { | ||
player.playSound(entity, sound, category, volume, pitch); | ||
} else if (ENTITY_EMITTER_SOUND) { | ||
Sound enumSound; | ||
try { | ||
enumSound = Sound.valueOf(sound.replace('.','_').toUpperCase(Locale.ENGLISH)); | ||
} catch (IllegalArgumentException e) { | ||
return; | ||
} | ||
player.playSound(entity, enumSound, category, volume, pitch); | ||
} else { | ||
player.playSound(entity.getLocation(), sound, category, volume, pitch); | ||
} | ||
} | ||
|
||
@Override | ||
public void playSound(Entity entity, NamespacedKey sound, SoundCategory category, float volume, float pitch, OptionalLong seed) { | ||
//noinspection DuplicatedCode | ||
if (ADVENTURE_API) { | ||
AdventureSoundUtils.playSound(player, entity, sound, category, volume, pitch, seed); | ||
} else if (!SPIGOT_SOUND_SEED || seed.isEmpty()) { | ||
this.playSound(entity, sound.getKey(), category, volume, pitch); | ||
} else { | ||
player.playSound(entity, sound.getKey(), category, volume, pitch, seed.getAsLong()); | ||
} | ||
} | ||
} | ||
|
||
// World adapter pattern | ||
class WorldSoundReceiver implements SoundReceiver { | ||
|
||
private final World world; | ||
|
||
protected WorldSoundReceiver(World world) { | ||
this.world = world; | ||
} | ||
|
||
@Override | ||
public void playSound(Location location, NamespacedKey sound, SoundCategory category, float volume, float pitch, OptionalLong seed) { | ||
//noinspection DuplicatedCode | ||
if (ADVENTURE_API) { | ||
AdventureSoundUtils.playSound(world, location, sound, category, volume, pitch, seed); | ||
} else if (!SPIGOT_SOUND_SEED || seed.isEmpty()) { | ||
world.playSound(location, sound.getKey(), category, volume, pitch); | ||
} else { | ||
world.playSound(location, sound.getKey(), category, volume, pitch, seed.getAsLong()); | ||
} | ||
} | ||
|
||
private void playSound(Entity entity, String sound, SoundCategory category, float volume, float pitch) { | ||
//noinspection DuplicatedCode | ||
if (ENTITY_EMITTER_STRING) { | ||
world.playSound(entity, sound, category, volume, pitch); | ||
} else if (ENTITY_EMITTER_SOUND) { | ||
Sound enumSound; | ||
try { | ||
enumSound = Sound.valueOf(sound.replace('.','_').toUpperCase(Locale.ENGLISH)); | ||
} catch (IllegalArgumentException e) { | ||
return; | ||
} | ||
world.playSound(entity, enumSound, category, volume, pitch); | ||
} else { | ||
world.playSound(entity.getLocation(), sound, category, volume, pitch); | ||
} | ||
} | ||
|
||
@Override | ||
public void playSound(Entity entity, NamespacedKey sound, SoundCategory category, float volume, float pitch, OptionalLong seed) { | ||
//noinspection DuplicatedCode | ||
if (ADVENTURE_API) { | ||
AdventureSoundUtils.playSound(world, entity, sound, category, volume, pitch, seed); | ||
} else if (!SPIGOT_SOUND_SEED || seed.isEmpty()) { | ||
this.playSound(entity, sound.getKey(), category, volume, pitch); | ||
} else { | ||
world.playSound(entity, sound.getKey(), category, volume, pitch, seed.getAsLong()); | ||
} | ||
} | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/ch/njol/skript/bukkitutil/sounds/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/** | ||
* This file is part of Skript. | ||
* | ||
* Skript is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* Skript is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with Skript. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
* Copyright Peter Güttinger, SkriptLang team and contributors | ||
*/ | ||
@NonNullByDefault({DefaultLocation.PARAMETER, DefaultLocation.RETURN_TYPE, DefaultLocation.FIELD}) | ||
package ch.njol.skript.bukkitutil.sounds; | ||
|
||
import org.eclipse.jdt.annotation.DefaultLocation; | ||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.