Skip to content

Commit

Permalink
SoundTargetPattern: Volume & pitch support
Browse files Browse the repository at this point in the history
  • Loading branch information
xDec0de committed Nov 4, 2024
1 parent 92c6609 commit 8d8087c
Showing 1 changed file with 11 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import net.codersky.mcutils.crossplatform.MessageReceiver;
import net.codersky.mcutils.crossplatform.player.MCPlayer;
import net.codersky.mcutils.java.math.MCNumbers;
import net.codersky.mcutils.java.strings.MCStrings;
import net.codersky.mcutils.java.strings.pattern.TargetPattern;
import org.jetbrains.annotations.NotNull;
Expand All @@ -12,11 +13,17 @@ public class SoundTargetPattern implements TargetPattern {
@Override
public String process(@NotNull MessageReceiver target, @NotNull String string, boolean applyEventPatterns) {
return MCStrings.match(string, "<sound:", "/>", message -> {
if (!(target instanceof MCPlayer))
if (!(target instanceof final MCPlayer player))
return;
final MCPlayer player = (MCPlayer) target;
final String sound = message.indexOf(":") > 1 ? message : "minecraft:" + message;
player.playSound(sound);
final String[] soundInfo = message.split(";");
float volume = 1;
float pitch = 1;
if (soundInfo.length >= 1)
volume = MCNumbers.asNumber(soundInfo[0], 1f);
if (soundInfo.length >= 2)
pitch = MCNumbers.asNumber(soundInfo[1], 1f);
final String sound = soundInfo[soundInfo.length - 1];
player.playSound(sound.indexOf(":") > 1 ? sound : "minecraft:" + sound, volume, pitch);
});
}
}

0 comments on commit 8d8087c

Please sign in to comment.