Преглед на файлове

Ensure that we're not spamming sounds

TfT_02 преди 11 години
родител
ревизия
da1841eb9e

+ 47 - 0
src/main/java/com/gmail/nossr50/datatypes/player/McMMOPlayer.java

@@ -75,6 +75,9 @@ public class McMMOPlayer {
     private boolean abilityUse = true;
     private boolean godMode;
 
+    private Sound recentSound;
+    private int   lastSound;
+
     private final Map<AbilityType, Boolean> abilityMode     = new HashMap<AbilityType, Boolean>();
     private final Map<AbilityType, Boolean> abilityInformed = new HashMap<AbilityType, Boolean>();
 
@@ -442,6 +445,50 @@ public class McMMOPlayer {
         godMode = !godMode;
     }
 
+    /*
+     * Sounds
+     */
+
+    /**
+     * Play a sound at the players location.
+     * Sound will only get played if it's different
+     * compared to the previously played sound or if the cooldown has expired.
+     *
+     * @param sound    The Sound to play
+     * @param volume   Volume of the sound
+     * @param pitch    Pitch of the sound
+     * @param cooldown Cooldown time between sounds
+     */
+    public void playSound(Sound sound, float volume, float pitch, int cooldown) {
+        if (getRecentSound() == sound && !SkillUtils.cooldownExpired(getLastSound(), 1)) {
+            return;
+        }
+
+        setRecentSound(sound);
+        actualizeLastSound();
+        player.playSound(player.getLocation(), sound, volume, pitch);
+    }
+
+    public void playSound(Sound sound, float volume, float pitch) {
+        playSound(sound, volume, pitch, 1);
+    }
+
+    public Sound getRecentSound() {
+        return recentSound;
+    }
+
+    public void setRecentSound(Sound recentSound) {
+        this.recentSound = recentSound;
+    }
+
+    public int getLastSound() {
+        return lastSound;
+    }
+
+    public void actualizeLastSound() {
+        lastSound = (int) (System.currentTimeMillis() / Misc.TIME_CONVERSION_FACTOR);
+    }
+
     /*
      * Skill notifications
      */

+ 1 - 1
src/main/java/com/gmail/nossr50/skills/repair/RepairManager.java

@@ -148,7 +148,7 @@ public class RepairManager extends SkillManager {
 
         // BWONG BWONG BWONG
         if (Config.getInstance().getRepairAnvilUseSoundsEnabled()) {
-            player.playSound(player.getLocation(), Sound.ANVIL_USE, Misc.ANVIL_USE_VOLUME, Misc.ANVIL_USE_PITCH);
+            mcMMOPlayer.playSound(Sound.ANVIL_USE, Misc.ANVIL_USE_VOLUME, Misc.ANVIL_USE_PITCH);
         }
 
         // Repair the item!