From 04436731c67284f69e5b5101d82ef45f5ac81072 Mon Sep 17 00:00:00 2001 From: Pablo Herrera Date: Sun, 15 Sep 2024 22:26:03 +0200 Subject: [PATCH] Minimum start for sound keys Signed-off-by: Pablo Herrera --- .../pgm/platform/modern/ModernMiscUtil.java | 7 +++++++ .../pgm/platform/sportpaper/SpMiscUtil.java | 10 ++++++++++ .../main/java/tc/oc/pgm/util/Audience.java | 14 +++++--------- .../java/tc/oc/pgm/util/bukkit/MiscUtils.java | 4 ++++ .../java/tc/oc/pgm/util/bukkit/Sounds.java | 19 +++++++++++++++++++ 5 files changed, 45 insertions(+), 9 deletions(-) create mode 100644 util/src/main/java/tc/oc/pgm/util/bukkit/Sounds.java diff --git a/platform/platform-modern/src/main/java/tc/oc/pgm/platform/modern/ModernMiscUtil.java b/platform/platform-modern/src/main/java/tc/oc/pgm/platform/modern/ModernMiscUtil.java index db2371da24..9df85b50b4 100644 --- a/platform/platform-modern/src/main/java/tc/oc/pgm/platform/modern/ModernMiscUtil.java +++ b/platform/platform-modern/src/main/java/tc/oc/pgm/platform/modern/ModernMiscUtil.java @@ -5,10 +5,12 @@ import com.google.gson.JsonObject; import java.nio.file.Path; import java.util.List; +import net.kyori.adventure.key.Key; import net.minecraft.nbt.NbtAccounter; import net.minecraft.nbt.NbtIo; import net.minecraft.nbt.NbtUtils; import org.bukkit.Location; +import org.bukkit.Sound; import org.bukkit.block.Block; import org.bukkit.craftbukkit.CraftWorld; import org.bukkit.craftbukkit.inventory.CraftItemStack; @@ -102,4 +104,9 @@ public int getWorldDataVersion(Path levelDat) { return -1; } } + + @Override + public Key getSound(Sound enumConstant) { + return enumConstant.key(); + } } diff --git a/platform/platform-sportpaper/src/main/java/tc/oc/pgm/platform/sportpaper/SpMiscUtil.java b/platform/platform-sportpaper/src/main/java/tc/oc/pgm/platform/sportpaper/SpMiscUtil.java index e2fabd1116..59bfe666b2 100644 --- a/platform/platform-sportpaper/src/main/java/tc/oc/pgm/platform/sportpaper/SpMiscUtil.java +++ b/platform/platform-sportpaper/src/main/java/tc/oc/pgm/platform/sportpaper/SpMiscUtil.java @@ -1,5 +1,6 @@ package tc.oc.pgm.platform.sportpaper; +import static net.kyori.adventure.key.Key.key; import static tc.oc.pgm.util.platform.Supports.Priority.HIGH; import static tc.oc.pgm.util.platform.Supports.Variant.SPORTPAPER; @@ -7,11 +8,14 @@ import java.nio.file.Files; import java.nio.file.Path; import java.util.List; +import net.kyori.adventure.key.Key; import net.minecraft.server.v1_8_R3.EntityPotion; import net.minecraft.server.v1_8_R3.NBTCompressedStreamTools; import net.minecraft.server.v1_8_R3.World; import org.bukkit.Location; +import org.bukkit.Sound; import org.bukkit.block.Block; +import org.bukkit.craftbukkit.v1_8_R3.CraftSound; import org.bukkit.craftbukkit.v1_8_R3.CraftWorld; import org.bukkit.craftbukkit.v1_8_R3.inventory.CraftItemStack; import org.bukkit.entity.Arrow; @@ -101,4 +105,10 @@ public int getWorldDataVersion(Path levelDat) { return -1; } } + + @Override + @SuppressWarnings("PatternValidation") + public Key getSound(Sound enumConstant) { + return key(CraftSound.getSound(enumConstant)); + } } diff --git a/util/src/main/java/tc/oc/pgm/util/Audience.java b/util/src/main/java/tc/oc/pgm/util/Audience.java index 75222b3964..f078aecd2b 100644 --- a/util/src/main/java/tc/oc/pgm/util/Audience.java +++ b/util/src/main/java/tc/oc/pgm/util/Audience.java @@ -1,7 +1,5 @@ package tc.oc.pgm.util; -import static net.kyori.adventure.key.Key.key; -import static net.kyori.adventure.sound.Sound.sound; import static net.kyori.adventure.text.Component.text; import java.util.ArrayList; @@ -12,31 +10,29 @@ import java.util.stream.Collectors; import net.kyori.adventure.audience.ForwardingAudience; import net.kyori.adventure.platform.bukkit.BukkitAudiences; -import net.kyori.adventure.sound.Sound; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.ComponentLike; import net.kyori.adventure.text.format.NamedTextColor; import org.bukkit.command.CommandSender; import org.jetbrains.annotations.NotNull; import tc.oc.pgm.util.bukkit.BukkitUtils; +import tc.oc.pgm.util.bukkit.Sounds; import tc.oc.pgm.util.text.ComponentRenderer; /** Receiver of chat messages, sounds, titles, and other media. */ @FunctionalInterface public interface Audience extends ForwardingAudience.Single { - Sound WARNING_SOUND = sound(key("note.bass"), Sound.Source.MASTER, 1f, 0.75f); Component WARNING_MESSAGE = text(" \u26a0 ", NamedTextColor.YELLOW); // ⚠ default void sendWarning(ComponentLike message) { sendMessage(WARNING_MESSAGE.append(message.asComponent().colorIfAbsent(NamedTextColor.RED))); - playSound(WARNING_SOUND); + playSound(Sounds.WARNING); } - BukkitAudiences PROVIDER = - BukkitAudiences.builder(BukkitUtils.getPlugin()) - .componentRenderer(ComponentRenderer.RENDERER) - .build(); + BukkitAudiences PROVIDER = BukkitAudiences.builder(BukkitUtils.getPlugin()) + .componentRenderer(ComponentRenderer.RENDERER) + .build(); static Audience console() { return PROVIDER::console; diff --git a/util/src/main/java/tc/oc/pgm/util/bukkit/MiscUtils.java b/util/src/main/java/tc/oc/pgm/util/bukkit/MiscUtils.java index dc179fa62a..7f333c5ab8 100644 --- a/util/src/main/java/tc/oc/pgm/util/bukkit/MiscUtils.java +++ b/util/src/main/java/tc/oc/pgm/util/bukkit/MiscUtils.java @@ -3,7 +3,9 @@ import com.google.gson.JsonObject; import java.nio.file.Path; import java.util.List; +import net.kyori.adventure.key.Key; import org.bukkit.Location; +import org.bukkit.Sound; import org.bukkit.block.Block; import org.bukkit.entity.Arrow; import org.bukkit.entity.Entity; @@ -51,4 +53,6 @@ EntityChangeBlockEvent createEntityChangeBlockEvent( double getArrowDamage(Arrow arrow); int getWorldDataVersion(Path levelDat); + + Key getSound(Sound constant); } diff --git a/util/src/main/java/tc/oc/pgm/util/bukkit/Sounds.java b/util/src/main/java/tc/oc/pgm/util/bukkit/Sounds.java new file mode 100644 index 0000000000..ab0700b8c9 --- /dev/null +++ b/util/src/main/java/tc/oc/pgm/util/bukkit/Sounds.java @@ -0,0 +1,19 @@ +package tc.oc.pgm.util.bukkit; + +import static net.kyori.adventure.sound.Sound.sound; +import static tc.oc.pgm.util.bukkit.MiscUtils.MISC_UTILS; + +import net.kyori.adventure.sound.Sound; + +public interface Sounds { + Sound WARNING = sound("NOTE_BASS", "BLOCK_NOTE_BLOCK_BASS", 1f, 0.75f); + + static Sound sound(String legacyConstant, String modernConstant, float volume, float pitch) { + return sound( + MISC_UTILS.getSound( + BukkitUtils.parse(org.bukkit.Sound::valueOf, legacyConstant, modernConstant)), + Sound.Source.MASTER, + volume, + pitch); + } +}