Skip to content

Commit

Permalink
Added sound component to messages (#1084)
Browse files Browse the repository at this point in the history
  • Loading branch information
OmerBenGera committed Apr 30, 2022
1 parent 5a1c68a commit 4bbd574
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 0 deletions.
Expand Up @@ -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;
Expand Down Expand Up @@ -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")));

Expand Down
@@ -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);
}

}
Expand Up @@ -324,6 +324,7 @@ public static InputStream getResource(String resourcePath) {
}
}

@Nullable
public static SoundWrapper getSound(ConfigurationSection section) {
if (section == null)
return null;
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

}

0 comments on commit 4bbd574

Please sign in to comment.