Merge pull request #75

* Add option to play a sound when a leash snaps

* Add option to configure the snap sound

* Replace for loop

* WARNING: Code has been updated

* Support custom sounds
This commit is contained in:
JustDoom authored and GitHub committed 2024-06-26 15:18:31 +02:00
1 parent 82cbc90a57
commit b99e5f4063
1 file changed
+48
@@ -0,0 +1,48 @@
package org.purpurmc.purpurextras.modules;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.sound.Sound;
import org.bukkit.NamespacedKey;
import org.bukkit.SoundCategory;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityUnleashEvent;
import org.purpurmc.purpurextras.PurpurExtras;
import java.util.Locale;
/**
* Adds a sound for when the leash snaps
*/
public class LeashSnapSoundModule implements PurpurExtrasModule, Listener {
private String sound;
private double volume;
private double pitch;
protected LeashSnapSoundModule() {
sound = PurpurExtras.getPurpurConfig().getString("settings.leash-snap.sound", "minecraft:block.bamboo.break");
volume = PurpurExtras.getPurpurConfig().getDouble("settings.leash-snap.volume", 1f);
pitch = PurpurExtras.getPurpurConfig().getDouble("settings.leash-snap.pitch", 1.25f);
}
@Override
public void enable() {
PurpurExtras plugin = PurpurExtras.getInstance();
plugin.getServer().getPluginManager().registerEvents(this, plugin);
}
@Override
public boolean shouldEnable() {
return PurpurExtras.getPurpurConfig().getBoolean("settings.leash-snap.enabled", false) && sound != null;
}
@EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true)
public void onLeashBreak(EntityUnleashEvent event){
if (event.getReason() != EntityUnleashEvent.UnleashReason.DISTANCE) return;
event.getEntity().getWorld().playSound(event.getEntity().getLocation(), sound, SoundCategory.PLAYERS, (float) volume, (float) pitch);
}
}