From 8c249da8d5d9f4c64b7ccf20dce8e5a36199dade Mon Sep 17 00:00:00 2001 From: xDec0de_ Date: Fri, 11 Oct 2024 23:20:12 +0200 Subject: [PATCH] Added #playSound methods for MCPlayers --- .../mcutils/spigot/player/SpigotPlayer.java | 9 +++++++ .../velocity/player/VelocityPlayer.java | 10 ++++++++ .../crossplatform/player/MCPlayer.java | 25 +++++++++++++++++++ 3 files changed, 44 insertions(+) diff --git a/platforms/spigot/src/main/java/net/codersky/mcutils/spigot/player/SpigotPlayer.java b/platforms/spigot/src/main/java/net/codersky/mcutils/spigot/player/SpigotPlayer.java index d9596e2..a09f9c6 100644 --- a/platforms/spigot/src/main/java/net/codersky/mcutils/spigot/player/SpigotPlayer.java +++ b/platforms/spigot/src/main/java/net/codersky/mcutils/spigot/player/SpigotPlayer.java @@ -1,11 +1,14 @@ package net.codersky.mcutils.spigot.player; import net.codersky.mcutils.crossplatform.player.MCPlayer; +import net.kyori.adventure.sound.Sound; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.serializer.bungeecord.BungeeComponentSerializer; import net.md_5.bungee.api.ChatMessageType; import net.md_5.bungee.api.chat.BaseComponent; import net.md_5.bungee.api.chat.TextComponent; +import org.bukkit.Keyed; +import org.bukkit.NamespacedKey; import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; @@ -57,6 +60,12 @@ public boolean sendActionBar(@NotNull Component message) { return true; } + @Override + public boolean playSound(@NotNull Sound sound) { + handle.playSound(handle.getLocation(), sound.name().asString(), sound.volume(), sound.pitch()); + return true; + } + /* * MessageReceiver implementation */ diff --git a/platforms/velocity/src/main/java/net/codersky/mcutils/velocity/player/VelocityPlayer.java b/platforms/velocity/src/main/java/net/codersky/mcutils/velocity/player/VelocityPlayer.java index 9964129..db4c6c1 100644 --- a/platforms/velocity/src/main/java/net/codersky/mcutils/velocity/player/VelocityPlayer.java +++ b/platforms/velocity/src/main/java/net/codersky/mcutils/velocity/player/VelocityPlayer.java @@ -2,6 +2,8 @@ import com.velocitypowered.api.proxy.Player; import net.codersky.mcutils.crossplatform.player.MCPlayer; +import net.kyori.adventure.key.Key; +import net.kyori.adventure.sound.Sound; import net.kyori.adventure.text.Component; import org.jetbrains.annotations.NotNull; @@ -52,6 +54,14 @@ public boolean sendActionBar(@NotNull Component message) { return true; } + // Sound // + + @Override + public boolean playSound(@NotNull Sound sound) { + handle.playSound(sound); + return true; + } + /* * MessageReceiver implementation */ diff --git a/shared/src/main/java/net/codersky/mcutils/crossplatform/player/MCPlayer.java b/shared/src/main/java/net/codersky/mcutils/crossplatform/player/MCPlayer.java index e4a5a63..6fea57e 100644 --- a/shared/src/main/java/net/codersky/mcutils/crossplatform/player/MCPlayer.java +++ b/shared/src/main/java/net/codersky/mcutils/crossplatform/player/MCPlayer.java @@ -4,6 +4,9 @@ import net.codersky.mcutils.crossplatform.MessageReceiver; import net.codersky.mcutils.java.strings.Replacement; import net.codersky.mcutils.java.strings.Replacer; +import net.kyori.adventure.key.Key; +import net.kyori.adventure.key.KeyPattern; +import net.kyori.adventure.sound.Sound; import net.kyori.adventure.text.Component; import org.jetbrains.annotations.NotNull; @@ -170,6 +173,28 @@ default boolean sendActionBar(@NotNull Component message, @NotNull Object... rep return sendActionBar(message, new Replacer(replacements)); } + /* + * Sound + */ + + boolean playSound(@NotNull Sound sound); + + default boolean playSound(@KeyPattern @NotNull String key, @NotNull Sound.Source source, float volume, float pitch) { + return playSound(Sound.sound(Key.key(key), source, volume, pitch)); + } + + default boolean playSound(@KeyPattern @NotNull String key, float volume, float pitch) { + return playSound(key, Sound.Source.AMBIENT, volume, pitch); + } + + default boolean playSound(@KeyPattern @NotNull String key, float volume) { + return playSound(key, volume, 1.0f); + } + + default boolean playSound(@KeyPattern @NotNull String key) { + return playSound(key, 1.0f); + } + /* * Replacement */