Skip to content

Commit

Permalink
Merge pull request #75
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
JustDoom authored Jun 26, 2024
1 parent 82cbc90 commit b99e5f4
Showing 1 changed file with 48 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit b99e5f4

Please sign in to comment.