Skip to content

Commit

Permalink
Add whatsplaying subcommand to /dsmm
Browse files Browse the repository at this point in the history
  • Loading branch information
OreCruncher committed Jan 4, 2025
1 parent a99a85a commit b9586e4
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ public class MusicManagerCommand extends AbstractClientCommand {
private static final String RESET = "reset";
private static final String PAUSE = "pause";
private static final String UNPAUSE = "unpause";
private static final String WHATS_PLAYING = "whatsplaying";

@Override
public void register(CommandDispatcher<ClientCommandRegistrationEvent.ClientCommandSourceStack> dispatcher, CommandBuildContext registryAccess) {
dispatcher.register(ClientCommandRegistrationEvent.literal(COMMAND)
.then(subCommand(RESET, MusicManagerCommandHandler::reset))
.then(subCommand(PAUSE, MusicManagerCommandHandler::pause))
.then(subCommand(UNPAUSE, MusicManagerCommandHandler::unpause)));
.then(subCommand(UNPAUSE, MusicManagerCommandHandler::unpause))
.then(subCommand(WHATS_PLAYING, MusicManagerCommandHandler::whatsPlaying)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import net.minecraft.network.chat.Component;
import org.orecruncher.dsurround.lib.GameUtils;
import org.orecruncher.dsurround.lib.config.ConfigurationData;
import org.orecruncher.dsurround.mixinutils.IMusicManager;

public class MusicManagerCommandHandler {
Expand Down Expand Up @@ -32,4 +33,13 @@ public static Component pause() {
return Component.translatable("dsurround.command.dsmm.pause.failure", t.getMessage());
}
}

public static Component whatsPlaying() {
try {
var result = ((IMusicManager)(GameUtils.getMC().getMusicManager())).dsurround_whatsPlaying();
return Component.translatable("dsurround.command.dsmm.whatsplaying.success", result);
} catch (Throwable t) {
return Component.translatable("dsurround.command.dsmm.whatsplaying.failure", t.getMessage());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import net.minecraft.client.resources.sounds.SoundInstance;
import net.minecraft.client.sounds.MusicManager;
import net.minecraft.network.chat.Component;
import net.minecraft.sounds.Music;
import org.jetbrains.annotations.Nullable;
import org.orecruncher.dsurround.gui.sound.SoundToast;
import org.orecruncher.dsurround.lib.gui.ColorPalette;
import org.orecruncher.dsurround.mixinutils.IMusicManager;
import org.orecruncher.dsurround.mixinutils.MixinHelpers;
import org.spongepowered.asm.mixin.Mixin;
Expand Down Expand Up @@ -64,6 +66,21 @@ public void dsurround_setPaused(boolean flag) {
}
}

@Override
public Component dsurround_whatsPlaying() {
if (this.currentMusic == null)
return Component.translatable("dsurround.text.musicmanager.nothing");

// Lookup meta information
var metaData = MixinHelpers.SOUND_LIBRARY.getSoundMetadata(this.currentMusic.getLocation());
if (metaData == null || Component.empty().equals(metaData.getTitle()))
return Component.literal(this.currentMusic.getLocation().toString());

var title = metaData.getTitle().copy().withColor(ColorPalette.PUMPKIN_ORANGE.getValue());
var author = metaData.getCredits().get(0).author().copy().withColor(ColorPalette.WHEAT.getValue());
return Component.translatable("dsurround.text.musicmanager.playing", title, author, Component.translationArg(this.currentMusic.getLocation()));
}

@Inject(method = "tick()V", at = @At("HEAD"), cancellable = true)
public void dsurround_pauseTickCheck(CallbackInfo ci) {
if (this.dsurround_pauseTicking)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package org.orecruncher.dsurround.mixinutils;

import net.minecraft.network.chat.Component;

public interface IMusicManager {

String dsurround_getDiagnosticText();

void dsurround_doCommand(String command);

void dsurround_setPaused(boolean flag);

Component dsurround_whatsPlaying();
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.orecruncher.dsurround.mixinutils;

import org.orecruncher.dsurround.Configuration;
import org.orecruncher.dsurround.config.libraries.ISoundLibrary;
import org.orecruncher.dsurround.config.libraries.ITagLibrary;
import org.orecruncher.dsurround.lib.di.ContainerManager;
import org.orecruncher.dsurround.lib.logging.IModLog;
Expand All @@ -12,6 +13,7 @@
public class MixinHelpers {
public static final IModLog LOGGER = ContainerManager.resolve(IModLog.class);
public static final ITagLibrary TAG_LIBRARY = ContainerManager.resolve(ITagLibrary.class);
public static final ISoundLibrary SOUND_LIBRARY = ContainerManager.resolve(ISoundLibrary.class);
public static final Configuration.SoundSystem soundSystemConfig = ContainerManager.resolve(Configuration.SoundSystem.class);
public static final Configuration.FootstepAccents footstepAccentsConfig = ContainerManager.resolve(Configuration.FootstepAccents.class);
public static final Configuration.ParticleTweaks particleTweaksConfig = ContainerManager.resolve(Configuration.ParticleTweaks.class);
Expand Down
6 changes: 5 additions & 1 deletion common/src/main/resources/assets/dsurround/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@
"dsurround.command.dsmm.pause.failure": "Unable to pause the Music Manager: %1$s",
"dsurround.command.dsmm.unpause.success": "Music Manager was unpaused",
"dsurround.command.dsmm.unpause.failure": "Unable to unpause the Music Manager: %1$s",
"dsurround.command.dsmm.whatsplaying.success": "\u00a72Playing\u00a7r: %1$s",
"dsurround.command.dsmm.whatsplaying.failure": "Unable to determine what the Music Manager is playing: %1$s",
"dsurround.config.title": "Dynamic Surroundings Configuration Options",
"dsurround.config.tooltip.clientRestartRequired": "CLIENT restart required",
"dsurround.config.tooltip.worldRestartRequired": "WORLD restart required",
Expand Down Expand Up @@ -209,5 +211,7 @@
"dsurround.text.reloadassets": "[%1$s\u00a7r] \u00a7aReloaded configurations",
"dsurround.text.seasons.spring": "Spring",
"dsurround.text.toast.music.title": "Music \"%1$s\"",
"dsurround.text.toast.music.author": "by %1$s"
"dsurround.text.toast.music.author": "by %1$s",
"dsurround.text.musicmanager.nothing": "No music is playing",
"dsurround.text.musicmanager.playing": "%1$s by %2$s (%3$s)"
}

0 comments on commit b9586e4

Please sign in to comment.