Skip to content

Commit

Permalink
Refactor EffPlaySound to be more readable and resistant to versioning…
Browse files Browse the repository at this point in the history
… 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
3 people authored Sep 1, 2024
1 parent 3843781 commit e0862a8
Show file tree
Hide file tree
Showing 6 changed files with 307 additions and 233 deletions.
108 changes: 0 additions & 108 deletions src/main/java/ch/njol/skript/bukkitutil/AdventureSoundReceiver.java

This file was deleted.

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 src/main/java/ch/njol/skript/bukkitutil/sounds/SoundReceiver.java
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 src/main/java/ch/njol/skript/bukkitutil/sounds/package-info.java
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;

22 changes: 10 additions & 12 deletions src/main/java/ch/njol/skript/config/SectionNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,6 @@
*/
package ch.njol.skript.config;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.regex.Pattern;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;

import ch.njol.skript.Skript;
import ch.njol.skript.SkriptAPIException;
import ch.njol.skript.config.validate.EntryValidator;
Expand All @@ -39,6 +27,16 @@
import ch.njol.util.NullableChecker;
import ch.njol.util.coll.CollectionUtils;
import ch.njol.util.coll.iterator.CheckedIterator;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.regex.Pattern;

/**
* @author Peter Güttinger
Expand Down
Loading

0 comments on commit e0862a8

Please sign in to comment.