Add sound effects

This commit is contained in:
William committed 2022-08-24 20:19:10 +01:00
1 parent 7b1930e554
commit 7d4dc6f11d
5 files changed
+33 -4

No files matched your search

@@ -129,6 +129,9 @@ public class Settings {
);
public Optional<String> getSoundEffect(@NotNull SoundEffectAction action) {
if (!playSoundEffects) {
return Optional.empty();
}
return Optional.ofNullable(soundEffects.get(action.name().toLowerCase()));
}
@@ -271,9 +274,9 @@ public class Settings {
* Represents the sound effect to play when an action happens
*/
public enum SoundEffectAction {
TELEPORTATION_COMPLETE("entity_enderman_teleport"),
TELEPORTATION_WARMUP("block_note_block_banjo"),
TELEPORTATION_CANCELLED("entity_item_break");
TELEPORTATION_COMPLETE("entity.enderman.teleport"),
TELEPORTATION_WARMUP("block.note_block.banjo"),
TELEPORTATION_CANCELLED("entity.item.break");
private final String defaultSoundEffect;
SoundEffectAction(@NotNull String defaultSoundEffect) {
@@ -2,6 +2,7 @@ package net.william278.huskhomes.listener;
import de.themoep.minedown.MineDown;
import net.william278.huskhomes.HuskHomes;
import net.william278.huskhomes.config.Settings;
import net.william278.huskhomes.player.OnlineUser;
import net.william278.huskhomes.position.Position;
import net.william278.huskhomes.teleport.TeleportType;
@@ -71,6 +72,8 @@ public class EventListener {
} else {
plugin.getLocales().getLocale("teleporting_complete")
.ifPresent(onlineUser::sendMessage);
plugin.getSettings().getSoundEffect(Settings.SoundEffectAction.TELEPORTATION_COMPLETE)
.ifPresent(onlineUser::playSound);
}
}).thenRun(() -> plugin.getDatabase().setCurrentTeleport(onlineUser, null))
.join();
@@ -82,6 +82,13 @@ public abstract class OnlineUser extends User {
*/
public abstract void sendMinecraftMessage(@NotNull String translationKey);
/**
* Play the specified sound to this player
*
* @param soundEffect the sound effect to play. If the sound name is invalid, the sound will not play
*/
public abstract void playSound(@NotNull String soundEffect);
/**
* Teleport a player to the specified {@link Location}
*
@@ -290,8 +290,13 @@ public class TeleportManager {
.ifPresent(onlineUser::sendMessage);
}
// Handle economy actions
// If the result was successful
if (teleportResult.successful) {
// Play sound
plugin.getSettings().getSoundEffect(Settings.SoundEffectAction.TELEPORTATION_COMPLETE)
.ifPresent(onlineUser::playSound);
// Handle economy actions
for (final Settings.EconomyAction action : economyActions) {
plugin.performEconomyTransaction(onlineUser, action);
}
@@ -371,6 +376,8 @@ public class TeleportManager {
executor.scheduleAtFixedRate(() -> {
// Display countdown action bar message
if (teleport.timeLeft > 0) {
plugin.getSettings().getSoundEffect(Settings.SoundEffectAction.TELEPORTATION_WARMUP)
.ifPresent(sound -> teleport.getPlayer().playSound(sound));
plugin.getLocales().getLocale("teleporting_action_bar_countdown", Integer.toString(teleport.timeLeft))
.ifPresent(message -> {
switch (plugin.getSettings().teleportWarmupDisplay) {
@@ -425,6 +432,8 @@ public class TeleportManager {
teleport.getPlayer().sendMessage(locale));
plugin.getLocales().getLocale("teleporting_action_bar_cancelled").ifPresent(locale ->
teleport.getPlayer().sendActionBar(locale));
plugin.getSettings().getSoundEffect(Settings.SoundEffectAction.TELEPORTATION_CANCELLED)
.ifPresent(sound -> teleport.getPlayer().playSound(sound));
teleport.cancelled = true;
return Optional.of(teleport);
}
@@ -435,6 +444,8 @@ public class TeleportManager {
teleport.getPlayer().sendMessage(locale));
plugin.getLocales().getLocale("teleporting_action_bar_cancelled").ifPresent(locale ->
teleport.getPlayer().sendActionBar(locale));
plugin.getSettings().getSoundEffect(Settings.SoundEffectAction.TELEPORTATION_CANCELLED)
.ifPresent(sound -> teleport.getPlayer().playSound(sound));
teleport.cancelled = true;
return Optional.of(teleport);
}