Skip to content

Commit 12bf771

Browse files
committed
add a new music implementation, has looping and insists on a single track at a time, need to make proper planning for switching tracks based on dynamic stuff like chat messages not just region
1 parent e1903a8 commit 12bf771

8 files changed

Lines changed: 130 additions & 1 deletion

File tree

src/main/java/dev/lycanea/mwonmod/Mwonmod.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package dev.lycanea.mwonmod;
22

3+
import dev.lycanea.mwonmod.music.CustomMusicManager;
34
import dev.lycanea.mwonmod.util.*;
45
import dev.lycanea.mwonmod.util.region.*;
56
import dev.lycanea.mwonmod.util.discord.DiscordManager;
@@ -105,6 +106,7 @@ public void onInitializeClient() {
105106
}
106107
players = client.player.getScoreboardTeam().getPlayerList().stream().toList();
107108
}
109+
CustomMusicManager.tick(client);
108110
});
109111

110112
UseEntityCallback.EVENT.register((SellEvent::entityInteract));
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package dev.lycanea.mwonmod.music;
2+
3+
import net.minecraft.client.MinecraftClient;
4+
import net.minecraft.client.sound.SoundInstance;
5+
import net.minecraft.sound.SoundEvent;
6+
import net.minecraft.util.Identifier;
7+
8+
public class CustomMusicManager {
9+
private static CustomSong currentSong = CustomSong.NONE;
10+
private static CustomSong playingSong = CustomSong.NONE;
11+
private static SoundInstance currentSound = null;
12+
13+
public static void setCurrentSong(CustomSong song) {
14+
currentSong = song;
15+
}
16+
17+
public static void tick(MinecraftClient client) {
18+
// client.options.getSoundVolumeOption(SoundCategory.MUSIC).setValue(0.0);
19+
20+
if (currentSong != playingSong) {
21+
stopCurrentSong(client);
22+
23+
if (currentSong != CustomSong.NONE) {
24+
playSong(client, currentSong);
25+
}
26+
27+
playingSong = currentSong;
28+
}
29+
}
30+
31+
private static void playSong(MinecraftClient client, CustomSong song) {
32+
Identifier soundId = song.getSoundId();
33+
if (soundId == null) return;
34+
35+
SoundEvent soundEvent = SoundEvent.of(soundId);
36+
LoopingSoundInstance sound = new LoopingSoundInstance(soundEvent);
37+
currentSound = sound;
38+
39+
client.getSoundManager().play(sound);
40+
}
41+
42+
public static void stopCurrentSong(MinecraftClient client) {
43+
if (currentSound != null) {
44+
if (currentSound instanceof LoopingSoundInstance looping) {
45+
looping.stop();
46+
}
47+
client.getSoundManager().stop(currentSound);
48+
currentSound = null;
49+
}
50+
}
51+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package dev.lycanea.mwonmod.music;
2+
3+
import net.minecraft.util.Identifier;
4+
5+
public enum CustomSong {
6+
NONE,
7+
SONG_ONE;
8+
9+
public Identifier getSoundId() {
10+
return switch (this) {
11+
case SONG_ONE -> Identifier.of("mwonmod:song_one");
12+
default -> null;
13+
};
14+
}
15+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package dev.lycanea.mwonmod.music;
2+
3+
import net.minecraft.client.sound.AbstractSoundInstance;
4+
import net.minecraft.client.sound.TickableSoundInstance;
5+
import net.minecraft.sound.SoundCategory;
6+
import net.minecraft.sound.SoundEvent;
7+
import net.minecraft.util.math.random.Random;
8+
9+
public class LoopingSoundInstance extends AbstractSoundInstance implements TickableSoundInstance {
10+
private boolean done = false;
11+
12+
public LoopingSoundInstance(SoundEvent soundEvent) {
13+
super(soundEvent, SoundCategory.MUSIC, Random.create());
14+
this.volume = 1.0f;
15+
this.pitch = 1.0f;
16+
this.repeat = true; // this the loopy part
17+
this.relative = true; // non-positional
18+
}
19+
20+
@Override
21+
public void tick() {
22+
// maybe add like music fading or something here?
23+
}
24+
25+
@Override
26+
public boolean isDone() {
27+
return done;
28+
}
29+
30+
public void stop() {
31+
done = true;
32+
}
33+
}

src/main/java/dev/lycanea/mwonmod/util/Commands.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import com.mojang.brigadier.CommandDispatcher;
55
import com.mojang.brigadier.arguments.StringArgumentType;
66
import dev.lycanea.mwonmod.Mwonmod;
7+
import dev.lycanea.mwonmod.music.CustomMusicManager;
8+
import dev.lycanea.mwonmod.music.CustomSong;
79
import dev.lycanea.mwonmod.util.region.Region;
810
import net.fabricmc.fabric.api.client.command.v2.ClientCommandManager;
911
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
@@ -73,6 +75,18 @@ public static void registerCommands(CommandDispatcher<FabricClientCommandSource>
7375
return 1;
7476
})
7577
)
78+
.then(literal("songtest")
79+
.executes(context -> {
80+
CustomMusicManager.setCurrentSong(CustomSong.SONG_ONE);
81+
return 1;
82+
})
83+
)
84+
.then(literal("songtest2")
85+
.executes(context -> {
86+
CustomMusicManager.setCurrentSong(CustomSong.NONE);
87+
return 1;
88+
})
89+
)
7690
.then(literal("region")
7791
.then(ClientCommandManager.argument("action", StringArgumentType.string())
7892
.then(ClientCommandManager.argument("name", StringArgumentType.string())

src/main/java/dev/lycanea/mwonmod/util/region/RegionUpdater.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package dev.lycanea.mwonmod.util.region;
22

33
import dev.lycanea.mwonmod.Mwonmod;
4+
import dev.lycanea.mwonmod.music.CustomMusicManager;
5+
import dev.lycanea.mwonmod.music.CustomSong;
46
import dev.lycanea.mwonmod.util.GameState;
57

68
import net.minecraft.client.MinecraftClient;
@@ -18,14 +20,15 @@
1820

1921
public class RegionUpdater {
2022

21-
public static final void init() {
23+
public static void init() {
2224

2325
AtomicReference<String> previousRegion = new AtomicReference<>();
2426

2527
ClientTickEvents.END_CLIENT_TICK.register(client -> {
2628
if (Mwonmod.onMelonKing()) {
2729
if (RegionLoader.getCurrentRegion() != null) {
2830
String regionName = RegionLoader.getCurrentRegion().name;
31+
CustomMusicManager.setCurrentSong(CustomSong.NONE);
2932
if (!Objects.equals(regionName, previousRegion.get())) {
3033
previousRegion.set(regionName);
3134
if (Objects.equals(regionName, "housing")) {
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"song_one": {
3+
"category": "music",
4+
"sounds": [
5+
{
6+
"name": "mwonmod:song_one",
7+
"stream": true
8+
}
9+
]
10+
}
11+
}
1010 KB
Binary file not shown.

0 commit comments

Comments
 (0)