From 4bbd574cf140c0ab782dbce0817c5fb71b027f12 Mon Sep 17 00:00:00 2001 From: Ome_R Date: Sat, 30 Apr 2022 19:25:54 +0300 Subject: [PATCH] Added sound component to messages (#1084) --- .../lang/component/MultipleComponents.java | 4 +++ .../lang/component/impl/SoundComponent.java | 34 +++++++++++++++++++ .../superiorskyblock/utils/FileUtils.java | 1 + .../wrappers/SoundWrapper.java | 6 ++++ 4 files changed, 45 insertions(+) create mode 100644 src/main/java/com/bgsoftware/superiorskyblock/lang/component/impl/SoundComponent.java diff --git a/src/main/java/com/bgsoftware/superiorskyblock/lang/component/MultipleComponents.java b/src/main/java/com/bgsoftware/superiorskyblock/lang/component/MultipleComponents.java index ea9499943..c8eb228f8 100644 --- a/src/main/java/com/bgsoftware/superiorskyblock/lang/component/MultipleComponents.java +++ b/src/main/java/com/bgsoftware/superiorskyblock/lang/component/MultipleComponents.java @@ -3,7 +3,9 @@ import com.bgsoftware.superiorskyblock.formatting.Formatters; import com.bgsoftware.superiorskyblock.lang.component.impl.ActionBarComponent; import com.bgsoftware.superiorskyblock.lang.component.impl.ComplexMessageComponent; +import com.bgsoftware.superiorskyblock.lang.component.impl.SoundComponent; import com.bgsoftware.superiorskyblock.lang.component.impl.TitleComponent; +import com.bgsoftware.superiorskyblock.utils.FileUtils; import com.bgsoftware.superiorskyblock.utils.StringUtils; import net.md_5.bungee.api.chat.BaseComponent; import net.md_5.bungee.api.chat.ClickEvent; @@ -33,6 +35,8 @@ public static IMessageComponent parseSection(ConfigurationSection section) { section.getInt(key + ".duration"), section.getInt(key + ".fade-out") )); + } else if (key.equals("sound")) { + messageComponents.add(SoundComponent.of(FileUtils.getSound(section.getConfigurationSection("sound")))); } else { TextComponent textComponent = new TextComponent(Formatters.COLOR_FORMATTER.format(section.getString(key + ".text"))); diff --git a/src/main/java/com/bgsoftware/superiorskyblock/lang/component/impl/SoundComponent.java b/src/main/java/com/bgsoftware/superiorskyblock/lang/component/impl/SoundComponent.java new file mode 100644 index 000000000..5e6797ae9 --- /dev/null +++ b/src/main/java/com/bgsoftware/superiorskyblock/lang/component/impl/SoundComponent.java @@ -0,0 +1,34 @@ +package com.bgsoftware.superiorskyblock.lang.component.impl; + +import com.bgsoftware.superiorskyblock.lang.component.EmptyMessageComponent; +import com.bgsoftware.superiorskyblock.lang.component.IMessageComponent; +import com.bgsoftware.superiorskyblock.wrappers.SoundWrapper; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +import javax.annotation.Nullable; + +public final class SoundComponent implements IMessageComponent { + + private final SoundWrapper soundWrapper; + + public static IMessageComponent of(@Nullable SoundWrapper soundWrapper) { + return SoundWrapper.isEmpty(soundWrapper) ? EmptyMessageComponent.getInstance() : new SoundComponent(soundWrapper); + } + + private SoundComponent(SoundWrapper soundWrapper) { + this.soundWrapper = soundWrapper; + } + + @Override + public String getMessage() { + return ""; + } + + @Override + public void sendMessage(CommandSender sender, Object... objects) { + if (sender instanceof Player) + soundWrapper.playSound((Player) sender); + } + +} diff --git a/src/main/java/com/bgsoftware/superiorskyblock/utils/FileUtils.java b/src/main/java/com/bgsoftware/superiorskyblock/utils/FileUtils.java index 2f0fe46ff..c670fbfac 100644 --- a/src/main/java/com/bgsoftware/superiorskyblock/utils/FileUtils.java +++ b/src/main/java/com/bgsoftware/superiorskyblock/utils/FileUtils.java @@ -324,6 +324,7 @@ public static InputStream getResource(String resourcePath) { } } + @Nullable public static SoundWrapper getSound(ConfigurationSection section) { if (section == null) return null; diff --git a/src/main/java/com/bgsoftware/superiorskyblock/wrappers/SoundWrapper.java b/src/main/java/com/bgsoftware/superiorskyblock/wrappers/SoundWrapper.java index fb72c09be..c6ebfaf7e 100644 --- a/src/main/java/com/bgsoftware/superiorskyblock/wrappers/SoundWrapper.java +++ b/src/main/java/com/bgsoftware/superiorskyblock/wrappers/SoundWrapper.java @@ -4,6 +4,8 @@ import org.bukkit.entity.HumanEntity; import org.bukkit.entity.Player; +import javax.annotation.Nullable; + public final class SoundWrapper { private final Sound sound; @@ -40,4 +42,8 @@ public SoundWrapper copy() { return new SoundWrapper(this.sound, this.volume, this.pitch); } + public static boolean isEmpty(@Nullable SoundWrapper soundWrapper) { + return soundWrapper == null || soundWrapper.sound == null || soundWrapper.volume <= 0 || soundWrapper.pitch <= 0; + } + }